我有三個資料框:
AAA_PROSTATEx, BBB_PROSTATEx, CCC_PROSTATEx,
我想按如下方式處理和創建資料幀,但我不知道如何使用回圈來解決它們。理想情況下,會有兩個回圈:
### loop #1 ####
AAA_PROSTATEx_1 <- AAA_PROSTATEx[48:57, ]
BBB_PROSTATEx_1 <- BBB_PROSTATEx[48:57, ]
CCC_PROSTATEx_1 <- CCC_PROSTATEx[48:57, ]
# **name**_PROSTATEx_1 <- *name**_PROSTATEx[48:57, ]
### loop #2 ###
AAA_PROSTATEx_1 <-
AAA_PROSTATEx_1 %>%
mutate(V2.T2.Total = select(., V2.T2.Artef:V2.T2.Sag) %>%
rowSums()
)
BBB_PROSTATEx_1 <-
BBB_PROSTATEx_1 %>%
mutate(V2.T2.Total = select(., V2.T2.Artef:V2.T2.Sag) %>%
rowSums()
)
CCC_PROSTATEx_1 <-
CCC_PROSTATEx_1 %>%
mutate(V2.T2.Total = select(., V2.T2.Artef:V2.T2.Sag) %>%
rowSums()
)
# **name**_PROSTATEx_1 <-
**name**_PROSTATEx_1 %>%
mutate(V2.T2.Total = select(., V2.T2.Artef:V2.T2.Sag) %>%
rowSums()
)
uj5u.com熱心網友回復:
您可以通過字符名稱使用assign和get參考變數
tables <- c("AAA_PROSTATEx", "BBB_PROSTATEx", "CCC_PROSTATEx")
new_tables <- paste0(tables, "_1")
### loop #1 ####
for(i in seq_along(tables)){
assign(new_tables[i], get(tables[i])[48:57, ])
}
# **name**_PROSTATEx_1 <- *name**_PROSTATEx[48:57, ]
### loop #2 ###
for(i in seq_along(tables)){
assign(new_tables[i], get(new_tables[i]) %>%
mutate(V2.T2.Total = select(., V2.T2.Artef:V2.T2.Sag) %>%
rowSums()
))
}
但是,正如@tacoman 所建議的,最好使用資料框串列來代替
uj5u.com熱心網友回復:
如果你想保持簡單:
library(dplyr)
tables <- list(AAA_PROSTATEx, BBB_PROSTATEx, CCC_PROSTATEx)
lapply(tables, function(x) {
x %>%
dplyr::filter(dplyr::row_number() %in% 48:57) %>%
dplyr::mutate(V2.T2.Total = dplyr::select(.,V2.T2.Artef:V2.T2.Sag) %>%
rowSums())
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/518287.html
標籤:r循环数据表
