[英]Apply function taking matching columns in 2 data frames, looping over columns
[英]Apply succinct function over subsets of columns of data frames in a list
我有一個名為“ nldf”的數據幀(12列8行)的列表(28個項目)。 需要在每個數據幀內分別對列1:3、4:6、7:9、10:12逐行應用統計信息。 我正在遍歷列表,通過執行以下操作來計算統計信息:
library(tidyverse)
avgs <- n.l.df
avgs <- lapply(avgs, function(x) {
x[1,1] <-mean(as.numeric(x[1,1:3]))
x[2,1] <-mean(as.numeric(x[2,1:3]))
x[3,1] <-mean(as.numeric(x[3,1:3]))
x[4,1] <-mean(as.numeric(x[4,1:3]))
x[5,1] <-mean(as.numeric(x[5,1:3]))
x[6,1] <-mean(as.numeric(x[6,1:3]))
x[7,1] <-mean(as.numeric(x[7,1:3]))
x[8,1] <-mean(as.numeric(x[8,1:3]))
x[1,4] <-mean(as.numeric(x[1,4:6]))
x[2,4] <-mean(as.numeric(x[2,4:6]))
x[3,4] <-mean(as.numeric(x[3,4:6]))
x[4,4] <-mean(as.numeric(x[4,4:6]))
x[5,4] <-mean(as.numeric(x[5,4:6]))
x[6,4] <-mean(as.numeric(x[6,4:6]))
x[7,4] <-mean(as.numeric(x[7,4:6]))
x[8,4] <-mean(as.numeric(x[8,4:6]))
x[1,7] <-mean(as.numeric(x[1,7:9]))
x[2,7] <-mean(as.numeric(x[2,7:9]))
x[3,7] <-mean(as.numeric(x[3,7:9]))
x[4,7] <-mean(as.numeric(x[4,7:9]))
x[5,7] <-mean(as.numeric(x[5,7:9]))
x[6,7] <-mean(as.numeric(x[6,7:9]))
x[7,7] <-mean(as.numeric(x[7,7:9]))
x[8,7] <-mean(as.numeric(x[8,7:9]))
x[1,10] <-mean(as.numeric(x[1,10:12]))
x[2,10] <-mean(as.numeric(x[2,10:12]))
x[3,10] <-mean(as.numeric(x[3,10:12]))
x[4,10] <-mean(as.numeric(x[4,10:12]))
x[5,10] <-mean(as.numeric(x[5,10:12]))
x[6,10] <-mean(as.numeric(x[6,10:12]))
x[7,10] <-mean(as.numeric(x[7,10:12]))
x[8,10] <-mean(as.numeric(x[8,10:12]))
return(x)
})
這很好,我可以在需要時在第2、3、5、6、8、9、11和12列中刪除不必要的值。 我喜歡我不必將數據幀收集為長格式,並且最好將其保存為數據幀列表。
顯然,這種方式過於重復,我認為必須有一種方法來嵌套嵌套的lapply / apply,但這超出了我的水平。 如何簡化和縮短此代碼?
謝謝。
library(tidyverse)
# For reproducibility
set.seed(100)
# list of 28 random data frames
df_list <- rerun(28, data.frame(replicate(12,sample(1:100,8))))
# Use map to iterate over the list, using rowMeans and select to get means of select columns.
map(df_list, ~mutate(., rm_1_3 = rowMeans(select(., 1:3)),
rm_4_6 = rowMeans(select(., 4:6)),
rm_7_9 = rowMeans(select(., 7:9)),
rm_10_12 = rowMeans(select(., 10:12))))
[[1]]
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 rm_1_3 rm_4_6 rm_7_9 rm_10_12
1 31 55 21 43 35 34 21 13 45 58 46 31 35.66667 37.33333 26.33333 45.00000
2 26 17 36 17 95 86 31 23 36 96 60 73 26.33333 66.00000 30.00000 76.33333
3 55 62 99 76 69 77 33 59 100 65 91 89 72.00000 74.00000 64.00000 81.66667
4 6 86 67 86 87 81 20 21 44 61 96 21 53.00000 84.66667 28.33333 59.33333
5 45 27 52 53 18 58 23 45 24 83 4 35 41.33333 43.00000 30.66667 40.66667
6 46 38 68 27 60 47 27 62 66 74 55 43 50.66667 44.66667 51.66667 57.33333
7 77 72 51 46 94 74 56 91 39 79 69 86 66.66667 71.33333 62.00000 78.00000
8 35 63 70 87 13 83 24 63 31 9 24 37 56.00000 61.00000 39.33333 23.33333
這將為您提供28個數據幀的列表,並向每個數據幀添加4列統計信息。 如果您只想使用方法,請用transmute
代替mutate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.