[R] convert list to numeric

dadrivr dadrivr at gmail.com
Mon Nov 2 15:22:59 CET 2009


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 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:
> 
>>
>> 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!
>> --
>> View this message in context:
>> http://old.nabble.com/convert-list-to-numeric-tp26155039p26155039.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://old.nabble.com/convert-list-to-numeric-tp26155039p26157105.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list