简体   繁体   English

子集数据帧并使用loop或lapply存储到R中的不同变量

[英]Subset a data frame and store to different variables in R using loop or lapply

I have a data frame that I want to subset it several times and store it in different variable names. 我有一个数据框架,我想对其进行几次子集并将其存储在不同的变量名称中。 Let's say my data frame looks something like this: 假设我的数据框看起来像这样:

set.seed(123)
x <- rnorm(5)
y <- rnorm(5)
z <- rnorm(5)

f1 <- gl(2,1, labels = c("good", "bad"), length =5)
f2 <- gl(3,1, labels = c("red", "green", "yellow"), length = 5)
f3 <- gl(5,1, labels = c("foo", "bar", "foobar", "foofoo", "barbar"))

df <- data.frame(x,y,z,f1,f2,f3)    
> df

            x          y          z   f1     f2     f3
1 -0.56047565  1.7150650  1.2240818 good    red    foo
2 -0.23017749  0.4609162  0.3598138  bad  green    bar
3  1.55870831 -1.2650612  0.4007715 good yellow foobar
4  0.07050839 -0.6868529  0.1106827  bad    red foofoo
5  0.12928774 -0.4456620 -0.5558411 good  green barbar

What I want to do is to create three new data frames by subsetting df and store them to different variable names. 我想做的是通过设置df来创建三个新的数据帧,并将它们存储到不同的变量名中。 I know how to do that individually: 我知道如何单独执行此操作:

df_f1 <- df[,c(-5,-6)]

> df_f1
            x          y          z   f1
1 -0.56047565  1.7150650  1.2240818 good
2 -0.23017749  0.4609162  0.3598138  bad
3  1.55870831 -1.2650612  0.4007715 good
4  0.07050839 -0.6868529  0.1106827  bad
5  0.12928774 -0.4456620 -0.5558411 good

df_f2 <- df[,c(-4,-6)]

> df_f2
            x          y          z     f2
1 -0.56047565  1.7150650  1.2240818    red
2 -0.23017749  0.4609162  0.3598138  green
3  1.55870831 -1.2650612  0.4007715 yellow
4  0.07050839 -0.6868529  0.1106827    red
5  0.12928774 -0.4456620 -0.5558411  green

df_f3 <- df[,c(-4,-5)]
> df_f3
            x          y          z     f3
1 -0.56047565  1.7150650  1.2240818    foo
2 -0.23017749  0.4609162  0.3598138    bar
3  1.55870831 -1.2650612  0.4007715 foobar
4  0.07050839 -0.6868529  0.1106827 foofoo
5  0.12928774 -0.4456620 -0.5558411 barbar

However, is there a way to do it programmatically? 但是,有没有办法以编程方式进行此操作? Maybe using a for loop or lapply? 也许使用for循环或套用? My problem is that I don't know how can I assign the data frames I need to different variable names such as df_f1, df_f2 and df_f3 automatically without manually typing them one by one. 我的问题是我不知道如何自动将我需要的数据帧分配给不同的变量名称,例如df_f1,df_f2和df_f3,而无需手动手动键入它们。 What I mean is, is there a way to automatically generate variable names so that I can store data frames on them using loop or lapply? 我的意思是,有没有一种方法可以自动生成变量名,以便可以使用循环或lapply在它们上存储数据帧?

I will apply this concept to a bigger data set and manually typing each variable names is quite tedious. 我将这个概念应用于更大的数据集,并且手动键入每个变量名称非常繁琐。

Thanks and have a nice day to all! 谢谢,祝大家有美好的一天!

list2env(setNames(lapply(df[-(1:3)],cbind,df[1:3]),paste("df",1:3,sep="_f")),.GlobalEnv)

Breakdown: 分解:

First create a list that you need that has all the dataframes. 首先创建一个需要的列表,其中包含所有数据框。

  A=lapply(df[-(1:3)],cbind,df[1:3])

This takes all the other columns appart from 1:3, and then cbinds each one of the columns with df[1:3] . 这将所有其他列从1:3开始,然后将每个列与df[1:3]绑定。 This gives me a list A that hass all the dataframes I need. 这给了我一个列表A,其中包含我需要的所有数据帧。 Now Give every dataframe in the list A name: 现在给列表中的每个数据框一个名称:

  B=setNames(A,paste("df",1:3,sep="_f"))

You can play with paste to see how it combines two things together. 您可以玩paste ,看看它如何将两件事结合在一起。 After that. 之后。 We will list each element of the list, which is technically a dataframe to our global environment. 我们将列出列表中的每个元素,从技术上讲,这是我们全球环境的数据框架。

 list2env(B,.GlobalEnv)

This seems to work, using lapply : 这似乎有效,使用lapply

keep<-3
split_id<-(keep+1):length(df)
df_list<- lapply(split_id, function(x){
  df[,c(1:3,x)]
})

df_list
[[1]]
            x          y          z   f1
1 -0.56047565  1.7150650  1.2240818 good
2 -0.23017749  0.4609162  0.3598138  bad
3  1.55870831 -1.2650612  0.4007715 good
4  0.07050839 -0.6868529  0.1106827  bad
5  0.12928774 -0.4456620 -0.5558411 good

[[2]]
            x          y          z     f2
1 -0.56047565  1.7150650  1.2240818    red
2 -0.23017749  0.4609162  0.3598138  green
3  1.55870831 -1.2650612  0.4007715 yellow
4  0.07050839 -0.6868529  0.1106827    red
5  0.12928774 -0.4456620 -0.5558411  green

[[3]]
            x          y          z     f3
1 -0.56047565  1.7150650  1.2240818    foo
2 -0.23017749  0.4609162  0.3598138    bar
3  1.55870831 -1.2650612  0.4007715 foobar
4  0.07050839 -0.6868529  0.1106827 foofoo
5  0.12928774 -0.4456620 -0.5558411 barbar

Did you mean something like this? 你的意思是这样吗?

dependent_col = c("f1", "f2", "f3")
df_l <- lapply(dependent_col, function(x) df[!(colnames(df) %in% dependent_col) | colnames(df) == x])
names(df_l) <- paste("df", dependent_col, sep="_")
df_l

You can access individual dataframe using df_l$df_f1 , df_l$df_f2 etc... 您可以使用df_l$df_f1df_l$df_f2等访问单个数据帧...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM