Skip to content

convert list to numeric

7 messages · Benilton Carvalho, jim holtman, Petersen, Isaac T +2 more

#
I would like to preface this by saying that I am new to R, so I would ask
that you be patient and thorough, so that I'm not completely clueless.  I am
trying to convert a list to numeric so that I can perform computations on it 
(specifically mean-center the variable), but I am running into problems.  I
have imported the data set into "task" (data frame).  The data frame is made
of factors with variable names in the first row.  I am running a loop to set
a variable equal to a column in the data frame.  Here is an example of my
problem:

for (i in 1:dim(task)[2]){
predictor.loop <- c(task[i])
predictor.loop.mc <- predictor.loop - mean(predictor.loop, na.rm=T)
}

I get the following error: 
Error in predictor.loop - mean(predictor.loop, na.rm = T) : 
  non-numeric argument to binary operator
In addition: Warning message:
In mean.default(predictor.loop, na.rm = T) :
  argument is not numeric or logical: returning NA

The column is entirely made up of numerical data, except for the header,
which is a string.  My problem is that I receive an error because the
predictor.loop variable is not numerical, so I need to find a way to convert
it.  I tried using:
predictor.loop <- c(as.numeric(task[i]))
But I get the following error: "Error: (list) object cannot be coerced to
type 'double'"

If I call the variable, I can assign it to a numerical list (e.g., predictor
loop <- task$variablename), but since I am assigning the variable in a loop,
I have to find another way as the variable name would have to change in each
loop iteration.  Any help would be greatly appreciated.  Thanks!
#
it appears that what you really want is to use:

task[[i]]

instead of task[i]

b
On Nov 1, 2009, at 11:04 PM, dadrivr wrote:

            
#
Can you at least provide an 'str' of the 'task' object (not sure if it
is a dataframe; you said a 'list') so that we know what it looks like.
 It would also be helpful if you would provide a subset of the data
that we could try out a script on it.  The best way would be to post
the first 10 or so rows (and limit columns it more that 10 or so).  A
convenient way is to :

dput(task[1:10,])

and then cut/paste the result into your email.
On Sun, Nov 1, 2009 at 8:04 PM, dadrivr <dadrivr at gmail.com> wrote:

  
    
#
Great, that works very well.  What is the purpose of double brackets vs
single ones?  I will remember next time to include a subset of the data, so
that readers can run the script.  Thanks again for your help!
Benilton Carvalho wrote:

  
    
#
It's a way of extracting from a list. See help("[") or help("Extract")

Regards, Adai
dadrivr wrote:
#
It's a way of extracting from a list. See help("[") or help("Extract").
dadrivr wrote:
#
An example will help show the difference between single vs. double brackets.

## xl is  a list with three elements
$a
[1] 1 2 3

$b
[1] 2 3 4 5 6 7

$c
[1] "X" "Y"
## with single brackets, we get a subset. A subset of a list is still a list
$a
[1] 1 2 3

## extract the first element of xl, as itself, whatever it is
[1] 1 2 3

## with single brackets, we get a subset. In the next example the subset
## consists of the first and third elements of xl, i.e., a list 
having two elements
$a
[1] 1 2 3

$c
[1] "X" "Y"


## Similarly for data frames, and note how the formatting is 
different when the object
## is printed to the screen
a b c
1 1 2 X
2 2 3 Y
3 3 4 Z
[1] "data.frame"
b
1 2
2 3
3 4
[1] "data.frame"
[1] 2 3 4
[1] "integer"
At 6:22 AM -0800 11/2/09, dadrivr wrote: