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

问题描述 投票:0回答:3

我有一个数据框,我想多次将其子集化,并将其存储在不同的变量名称中。假设我的数据框看起来像这样:

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

我想要做的是通过子集化df创建三个新数据帧并将它们存储到不同的变量名称。我知道如何单独做到这一点:

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

但是,有没有办法以编程方式执行此操作?也许使用for循环或lapply?我的问题是我不知道如何自动将所需的数据帧分配给不同的变量名称,如df_f1,df_f2和df_f3,而无需逐个手动输入。我的意思是,有没有办法自动生成变量名称,以便我可以使用循环或lapply存储数据框?

我将这个概念应用于更大的数据集,并手动键入每个变量名称是相当繁琐的。

谢谢,祝大家度过愉快的一天!

r
3个回答
0
投票
list2env(setNames(lapply(df[-(1:3)],cbind,df[1:3]),paste("df",1:3,sep="_f")),.GlobalEnv)

分解:

首先创建一个包含所有数据帧的列表。

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

这将从1:3获取所有其他列appart,然后使用df[1:3]将每个列绑定。这给了我一个包含我需要的所有数据帧的列表A.现在给列表中的每个数据帧命名:

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

你可以和paste一起玩,看看它如何将两件事结合在一起。之后。我们将列出列表中的每个元素,这在技术上是我们全球环境的数据帧。

 list2env(B,.GlobalEnv)

0
投票

这似乎工作,使用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

0
投票

你的意思是这样的吗?

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

您可以使用df_l$df_f1df_l$df_f2等访问单个数据框...

© www.soinside.com 2019 - 2024. All rights reserved.