Skip to content

Assign a list to one column of data frame

6 messages · Jialin Ma, Bert Gunter

#
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
# 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
# [[1]]
# [1] 1 2 3
# 
# [[2]]
# [1] 1
# 
# [[3]]
# NULL
# Warning message:
# In `[<-.data.frame`(`*tmp*`, , "new4", value = list(c(1, 2, 3),  :
# provided 3 variables to replace 1 variables
# Warning message:
# In `[<-.data.frame`(`*tmp*`, , "new3", value = list(c(1, 2, 3),  :
# provided 3 variables to replace 1 variables
# 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!
#
?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:
a b
1 1 a
2 2 b
3 3 c

## add a list column, protected by "I()"
## works as advertised
a b          new
1 1 a 1, 2, 3,....
2 2 b          foo
3 3 c 0.080349....
[[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:
#
If you see my previous example, I have tried something like
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:
#
Use list indexing, "[[" not "[" .
a b          new
1 1 a 1, 2, 3,....
2 2 b          foo
3 3 c 0.248115....
[[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:
#
Dear Bert,

This is awesome, thanks a lot!

Best,
Marlin
On Sun, 2016-12-11 at 06:52 -0800, Bert Gunter wrote:
#
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: