Subset dataframe by rows according with logical co

2019-08-04 04:39发布

问题:

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)