[R] convert list to numeric

jim holtman jholtman at gmail.com
Mon Nov 2 02:18:44 CET 2009


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:
>
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list