Subset dataframe by rows according with logical condition applied to column

半腔热情 提交于 2019-12-11 10:23:24

问题


This might be a trivial question, but I´m stuck on it since days without finding useful help in previous discussions.

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

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.

Example output

[[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

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.

e.g:

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.

Thank you


回答1:


Just split the dataframe:

dflist <- split(df, df[,1])

Creating seperate dataframes:

list2env(split(df, df[,1]), envir = .GlobalEnv)


来源:https://stackoverflow.com/questions/24201333/subset-dataframe-by-rows-according-with-logical-condition-applied-to-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!