[R] Assign a list to one column of data frame

Bert Gunter bgunter.4567 at gmail.com
Sun Dec 11 16:20:16 CET 2016


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 main
difference is that $ does not allow computed indices, whereas [[ does.
x$name is equivalent to x[["name", exact = FALSE]]. "

Also, when used for list selection, "[" returns a list, while "[["
returns whatever was selected.



-- Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Dec 11, 2016 at 7:01 AM, Marlin JL.M <marlin- at gmx.cn> wrote:
> 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          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]      [,2]
>> [1,] 0.2481156 0.2138564
>> [2,] 0.8598658 0.2898058
>> [3,] 0.5854885 0.4084578
>>
>>
>> Cheers,
>> Bert
>>
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming
>> along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>>
>> On Sun, Dec 11, 2016 at 3:54 AM, Marlin JL.M <marlin- at gmx.cn> wrote:
>> >
>> > 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
>> > "I()".
>> >
>> > Perhaps the title of my email is a little unclear, sorry for the
>> > case.
>> >
>> > Best,
>> > Marlin.
>> >
>> > On Sun, 2016-12-11 at 03:03 -0800, Bert Gunter wrote:
>> > > ?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 <- data.frame(a=1:3,b=letters[1:3])
>> > > > df
>> > >
>> > >   a b
>> > > 1 1 a
>> > > 2 2 b
>> > > 3 3 c
>> > >
>> > > ## add a list column, protected by "I()"
>> > > > df$new <- I(list(1:5,g = "foo", abb = matrix(runif(6),nr=3)))
>> > >
>> > > ## works as advertised
>> > > > df
>> > >
>> > >   a b          new
>> > > 1 1 a 1, 2, 3,....
>> > > 2 2 b          foo
>> > > 3 3 c 0.080349....
>> > >
>> > > > df$new
>> > >
>> > > [[1]]
>> > > [1] 1 2 3 4 5
>> > >
>> > > $g
>> > > [1] "foo"
>> > >
>> > > $abb
>> > >            [,1]       [,2]
>> > > [1,] 0.08034915 0.83671591
>> > > [2,] 0.43938440 0.06067429
>> > > [3,] 0.88196881 0.33461234
>> > >
>> > >
>> > > Cheers,
>> > > Bert
>> > >
>> > >
>> > > Bert Gunter
>> > >
>> > > "The trouble with having an open mind is that people keep coming
>> > > along
>> > > and sticking things into it."
>> > > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip
>> > > )
>> > >
>> > >
>> > > On Sat, Dec 10, 2016 at 7:18 PM, Marlin JL.M <marlin- at gmx.cn>
>> > > wrote:
>> > > > 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  setosa
>> > > > # 2          4.9         3.0          1.4         0.2  setosa
>> > > > # 3          4.7         3.2          1.3         0.2  setosa
>> > > >
>> > > > > mylist <- list(c(1,2,3),c(1),c())
>> > > > > mylist
>> > > >
>> > > > # [[1]]
>> > > > # [1] 1 2 3
>> > > > #
>> > > > # [[2]]
>> > > > # [1] 1
>> > > > #
>> > > > # [[3]]
>> > > > # NULL
>> > > >
>> > > > > n1 <- 'new1'
>> > > > > df$n1 <- mylist
>> > > > > n2 <- 'new2'
>> > > > > df[,n2] <- mylist
>> > > >
>> > > > # Warning message:
>> > > > # In `[<-.data.frame`(`*tmp*`, , "new4", value = list(c(1, 2,
>> > > > 3),  :
>> > > > # provided 3 variables to replace 1 variables
>> > > >
>> > > > > n3 <- 'new3'
>> > > > > df[,n3] <- I(mylist)
>> > > >
>> > > > # Warning message:
>> > > > # In `[<-.data.frame`(`*tmp*`, , "new3", value = list(c(1, 2,
>> > > > 3),  :
>> > > > # provided 3 variables to replace 1 variables
>> > > >
>> > > > > eval(parse(text = paste0("df$","new4","<- mylist")))
>> > > > > df
>> > > >
>> > > > # Sepal.Length Sepal.Width Petal.Length Petal.Width
>> > > > Species      n1
>> > > > new2 new3    new4
>> > > > # 1          5.1         3.5          1.4         0.2  setosa
>> > > > 1, 2,
>> > > > 3    1    1 1, 2, 3
>> > > > #
>> > > > 2          4.9         3.0          1.4         0.2  setosa
>> > > >    1
>> > > >     2    2       1
>> > > > #
>> > > > 3          4.7         3.2          1.3         0.2  setosa
>> > > > NULL
>> > > >     3    3    NULL
>> > > >
>> > > >
>> > > > The "eval" works correctly, however, if I want to avoid using
>> > > > "eval", what
>> > > > should I do?
>> > > >
>> > > > Thanks!
>> > > >
>> > > > ______________________________________________
>> > > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more,
>> > > > see
>> > > > https://stat.ethz.ch/mailman/listinfo/r-help
>> > > > PLEASE do read the posting guide http://www.R-project.org/posti
>> > > > ng-g
>> > > > uide.html
>> > > > and provide commented, minimal, self-contained, reproducible
>> > > > code.
>



More information about the R-help mailing list