根据应用于列的逻辑条件按行对数据帧进行子集设置 [英] Subset dataframe by rows according with logical condition applied to column
问题描述
这可能是一个微不足道的问题,但是我已经坚持了好几天,没有在先前的讨论中找到有用的帮助.
This might be a trivial question, but I´m stuck on it since days without finding useful help in previous discussions.
我有一个像这样的data.frame:
I have a data.frame like this:
pho 23 3 23 4 5 6 7 fat
pho 24 5 6 7 8 8 2 rew
pho 2 3 4 5 6 7 6 ogd
caf 23 34 5 6 78 4 tre
caf 45 56 76 6 5 5 tre
fra 3 4 5 6 2 4 rfe
fra 4 65 76 78 3 2 ytr
fra 54 6 7 8 23 5 rte
我想对data.frame进行子集化,以便仅将具有相似[1,1]元素的行保持在一起.我想将这些新的data.frames存储在一个列表中,其中每个元素分别称为每个data.frame的[1,1]对象.
I would like to subset the data.frame in order to keep together only the rows which have similar [1,1] element. I would like to store these new data.frames in a list, where each element is called as the respective [1,1] object of each data.frame.
示例输出
[[pho]]
pho 23 3 23 4 5 6 7 fat
pho 24 5 6 7 8 8 2 rew
pho 2 3 4 5 6 7 6 ogd
[[caf]]
caf 23 34 5 6 78 4 tre
caf 45 56 76 6 5 5 tre
[[fra]]
fra 3 4 5 6 2 4 rfe
fra 4 65 76 78 3 2 ytr
fra 54 6 7 8 23 5 rte
我想这样做是为了将函数应用于列表中的所有data.frames,而仅考虑某些列中的值.
I would like to do this in order to apply a function to all the data.frames in the list considering only the values in some columns.
例如:
lapply(list.containing.dataframes, FUN)
我在其他问题上没有找到任何帮助,老实说,我没有取得任何值得报告的进展. 如果您能提供我可以用来解决问题的链接/手册/建议,我将不胜感激.
I did not find any help in other questions, and to be honest I did not achieve any progress worthed to be reported. I would really appreciate if you could provide link/manuals/suggestions I could use to solve my problem.
谢谢
推荐答案
只需拆分数据框:
dflist <- split(df, df[,1])
创建单独的数据帧:
list2env(split(df, df[,1]), envir = .GlobalEnv)
这篇关于根据应用于列的逻辑条件按行对数据帧进行子集设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!