Dear Bert,
This is awesome, thanks a lot!
Best,
Marlin
On Sun, 2016-12-11 at 06:52 -0800, Bert Gunter wrote:
> Use list indexing, "[[" not "[" .
>
> > df <- data.frame(a=1:3,b=letters[1:3])
> > x <- "new"
> > df[[x]]<- I(list(1:5,g = "foo", abb = matrix(runif(6),nr=3)))
> > df
>
> a b
If you see my previous example, I have tried something like
> df[,n3] <- I(mylist)
However, in my case, the name of the new column is in a variable (by
user input) which can not be directly used by the dollar assign. On the
other hand, "[<-" does not work correctly even if I wrap the list into
"
Glad to help.
However, I need to publicly correct my misstatement. Both "[[" and "["
can be used and are useful for list indexing. As ?"[" clearly states,
the former selects only a single column, while the latter can select
several.
Also:
"Both [[ and $ select a single element of the list. The m
Use list indexing, "[[" not "[" .
> df <- data.frame(a=1:3,b=letters[1:3])
> x <- "new"
> df[[x]]<- I(list(1:5,g = "foo", abb = matrix(runif(6),nr=3)))
> df
a b new
1 1 a 1, 2, 3,
2 2 b foo
3 3 c 0.248115
> df$new
[[1]]
[1] 1 2 3 4 5
$g
[1] "foo"
$abb
[,1]
?data.frame says:
"If a list or data frame or matrix is passed to data.frame it is as if
each component or column had been passed as a separate argument
(except for matrices of class "model.matrix" and those protected by
I). "
So doing what Help says to do seems to do what you asked:
> df <- da
Dear all,
I want to assign a list to one column of data.frame where the name of the column
is a variable. I tried the following:
Using R version 3.3.2
> df <- iris[1:3, ]
> df
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 5.1 3.5 1.4 0.2 setos
6 matches
Mail list logo