开发者

How to use data frame as an element in list?

开发者 https://www.devze.com 2023-04-08 20:28 出处:网络
I tried to assign a data frame as an element to a list, but after the assignment I found that the data frame has been changed to a list with values from the first column of the开发者_StackOverflow中文

I tried to assign a data frame as an element to a list, but after the assignment I found that the data frame has been changed to a list with values from the first column of the开发者_StackOverflow中文版 data frame.

For example the following code :

dfm <- data.frame(x=1:2, y=3:4)
print(dfm)
l <- list()
l['Key'] <- dfm
print(l)

gives the output:

  x y
1 1 3
2 2 4
$Key
[1] 1 2

Is there a way to keep the data frame the way it is in a list ?


It would benefit you to pay attention to the warning message:

Warning message:
In l["Key"] <- dfm :
  number of items to replace is not a multiple of replacement length

You want to use [[ instead of [ (read ?Extract to understand why):

l[['Key']] <- dfm
0

精彩评论

暂无评论...
验证码 换一张
取 消