从数据框R列表中获取列 [英] get column from list of dataframes R
问题描述
dfList < split(mtcars,mtcars $ cyl)
现在我想检索一个特定数据框的列,例如数据框1中的列2,所以像
dfList [1] [2]
现在我可以做的是为循环创建以获取数据结构内部。但是,如果存在,我找不到一个oneliner来做。我怎样才能做到这一点?感谢提前!
我将docendo的评论放在这里,以完成这个问题。
如果要从列表中提取元素(并将其视为数据框架),而不是子集列表(创建较小的列表),则需要使用 [[]]
语法。另外,要从data.frame索引获取列,您需要使用 [[idx]]
或 [,idx]
。这些都是很基本的索引操作,如果您在R中进行编程,您可能需要查看这些操作。所以您的正确调用可能是
dfList [[1]] [[2]]
I am an R beginner and I am stuck on this problem. I had a dataframe and by using the split() function I have created a list of dataframes, e.g:
dfList <- split(mtcars, mtcars$cyl)
Now I want to retrieve a column of a specific dataframe, e.g. column 2 from dataframe 1, so something like
dfList[1][2]
What I can do right now is create for
loops to get inside the data structure. But I can't find a oneliner to do it, if it exists. How can I do that? Thanks in advance!
I'm putting docendo's comment here to close out the question.
If you want to extract an element from a list (and treat it like a data.frame) rather than subset a list (to create a smaller list), you need to use the [[ ]]
syntax. Plus, to get a column by index from a data.frame, you either need to use [[ idx ]]
or [, idx ]
. These are pretty basic indexing operations that you will probably want to review if you will be programming in R. So your "correct" call is probably
dfList[[1]][[2]]
这篇关于从数据框R列表中获取列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!