![](/img/trans.png)
[英]Using lapply to subset a single data frame into a list of data frames in R
[英]Subset a data frame and store to different variables in R using loop or lapply
我有一個數據框架,我想對其進行幾次子集並將其存儲在不同的變量名稱中。 假設我的數據框看起來像這樣:
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循環或套用? 我的問題是我不知道如何自動將我需要的數據幀分配給不同的變量名稱,例如df_f1,df_f2和df_f3,而無需手動手動鍵入它們。 我的意思是,有沒有一種方法可以自動生成變量名,以便可以使用循環或lapply在它們上存儲數據幀?
我將這個概念應用於更大的數據集,並且手動鍵入每個變量名稱非常繁瑣。
謝謝,祝大家有美好的一天!
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開始,然后將每個列與df[1:3]
綁定。 這給了我一個列表A,其中包含我需要的所有數據幀。 現在給列表中的每個數據框一個名稱:
B=setNames(A,paste("df",1:3,sep="_f"))
您可以玩paste
,看看它如何將兩件事結合在一起。 之后。 我們將列出列表中的每個元素,從技術上講,這是我們全球環境的數據框架。
list2env(B,.GlobalEnv)
這似乎有效,使用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
你的意思是這樣嗎?
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_f1
, df_l$df_f2
等訪問單個數據幀...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.