An example will help show the difference between single vs. double brackets.
## xl is a list with three elements
xl <- list( a=1:3, b=2:7, c=c('X','Y'))
xl
$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
xl[1]
$a
[1] 1 2 3
## extract the first element of xl, as itself, whatever it is
xl[[1]]
[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
xl[c(1,3)]
$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
xd <- data.frame( a=1:3, b=2:4, c=c('X','Y','Z'))
xd
a b c
1 1 2 X
2 2 3 Y
3 3 4 Z
class(xd)
[1] "data.frame"
xd[2]
b
1 2
2 3
3 4
class(xd[2])
[1] "data.frame"
xd[[2]]
[1] 2 3 4
class(xd[[2]])
[1] "integer"
At 6:22 AM -0800 11/2/09, dadrivr 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 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@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@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.
______________________________________________
R-help@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.
--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
______________________________________________
R-help@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.