使用 r 中的列增量创建 for 循环
Creating a for loop using increments of columns in r
我大约 3 周前才开始使用 r,我需要帮助。希望我能解释清楚。
我有一个数据集,其中 4 列代表对 1 个刺激的评级。有 55 个刺激,总共 220 列。我需要创建一个循环,以 4 为增量遍历 220 列,同时完成以下功能:
a<-rowMeans(dataset[1:4], na.rm = TRUE)
data.frame(a)
mean(a, na.rm = TRUE)
我需要列的每一行的总和以及总和的平均值。所以从本质上讲,我试图以一种简洁的方式实现以下目标:
a<-rowMeans(dataset[1:4], na.rm = TRUE)
data.frame(a)
mean(a, na.rm = TRUE)
b<-rowMeans(dataset[5:8], na.rm = TRUE)
data.frame(b)
mean(b, na.rm = TRUE)
c<-rowMeans(dataset[9:12], na.rm = TRUE)
data.frame(c)
mean(c, na.rm = TRUE)
请帮助让我的生活更轻松。我需要一个循环,所以我不必手动执行此操作 55 次。
提前致谢,
伊薇特
您可以为您的 rowmeans 创建一个新的 dataframe 并使用带有波纹管算法的循环:
D <- data.frame(c(1:55),rep(0,55))
for( i in 1:55 )
{
D[i,2]=Mean(rowMeans(dataset[,i*4-3:i*4],na.rm=TRUE),na.rm=TRUE)
}
Mean(dataset[,2],na.rm=TRUE)
实际上这段代码计算列的行均值。
您不需要 for-loop
。根据您为 dataset
设置的列的名称以及您希望最终结果的显示方式,可以有多种选择。
Base-R: 使用apply
和aggregate
的解决方案:
colMeans(t(apply(dataset, 1, function(x)
aggregate(x, by=list(0:(length(x)-1) %/% 4), mean, na.rm = TRUE)$x)), na.rm = TRUE)
#[1] 5.5 15.5 25.5
选项#2: 基于 tidyverse
的解决方案。您需要重命名列以表示 group
。然后使用 gather
将数据转换为长格式。现在,您可以将基于组的数据汇总为:
library(tidyverse)
dataset %>% setNames(paste("Group", ((0:(ncol(.)-1)) %/%4)+1, (1:ncol(.)),sep="_")) %>%
gather(Group, Value) %>%
mutate(Group = gsub("_\d+$","",Group)) %>%
group_by(Group) %>%
summarise(Value = mean(Value, na.rm = TRUE))
# # A tibble: 3 x 2
# Group Value
# <chr> <dbl>
# 1 Group_1 5.50
# 2 Group_2 15.5
# 3 Group_3 25.5
数据:
Data is created to represent just `12` columns of the dataset mentioned by OP.
set.seed(1)
dataset <- data.frame(col1 = sample(1:10,10),
col2 = sample(1:10,10),
col3 = sample(1:10,10),
col4 = sample(1:10,10),
col5 = sample(11:20,10),
col6 = sample(11:20,10),
col7 = sample(11:20,10),
col8 = sample(11:20,10),
col9 = sample(21:30,10),
col10 = sample(21:30,10),
col11 = sample(21:30,10),
col12 = sample(21:30,10)
)
dataset
# col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12
# 1 3 3 10 5 19 15 20 14 25 23 27 30
# 2 4 2 2 6 16 18 13 18 27 21 24 27
# 3 5 6 6 4 17 14 14 13 24 26 23 23
# 4 7 10 1 2 14 12 19 19 23 27 30 24
# 5 2 5 9 10 18 11 18 17 30 25 29 21
# 6 8 7 8 8 20 16 12 15 22 24 22 26
# 7 9 8 7 9 11 17 15 20 29 22 21 28
# 8 6 4 5 1 12 19 17 12 21 28 25 25
# 9 10 1 3 7 13 13 11 16 28 30 28 29
# 10 1 9 4 3 15 20 16 11 26 29 26 22
我大约 3 周前才开始使用 r,我需要帮助。希望我能解释清楚。
我有一个数据集,其中 4 列代表对 1 个刺激的评级。有 55 个刺激,总共 220 列。我需要创建一个循环,以 4 为增量遍历 220 列,同时完成以下功能:
a<-rowMeans(dataset[1:4], na.rm = TRUE)
data.frame(a)
mean(a, na.rm = TRUE)
我需要列的每一行的总和以及总和的平均值。所以从本质上讲,我试图以一种简洁的方式实现以下目标:
a<-rowMeans(dataset[1:4], na.rm = TRUE)
data.frame(a)
mean(a, na.rm = TRUE)
b<-rowMeans(dataset[5:8], na.rm = TRUE)
data.frame(b)
mean(b, na.rm = TRUE)
c<-rowMeans(dataset[9:12], na.rm = TRUE)
data.frame(c)
mean(c, na.rm = TRUE)
请帮助让我的生活更轻松。我需要一个循环,所以我不必手动执行此操作 55 次。
提前致谢,
伊薇特
您可以为您的 rowmeans 创建一个新的 dataframe 并使用带有波纹管算法的循环:
D <- data.frame(c(1:55),rep(0,55))
for( i in 1:55 )
{
D[i,2]=Mean(rowMeans(dataset[,i*4-3:i*4],na.rm=TRUE),na.rm=TRUE)
}
Mean(dataset[,2],na.rm=TRUE)
实际上这段代码计算列的行均值。
您不需要 for-loop
。根据您为 dataset
设置的列的名称以及您希望最终结果的显示方式,可以有多种选择。
Base-R: 使用apply
和aggregate
的解决方案:
colMeans(t(apply(dataset, 1, function(x)
aggregate(x, by=list(0:(length(x)-1) %/% 4), mean, na.rm = TRUE)$x)), na.rm = TRUE)
#[1] 5.5 15.5 25.5
选项#2: 基于 tidyverse
的解决方案。您需要重命名列以表示 group
。然后使用 gather
将数据转换为长格式。现在,您可以将基于组的数据汇总为:
library(tidyverse)
dataset %>% setNames(paste("Group", ((0:(ncol(.)-1)) %/%4)+1, (1:ncol(.)),sep="_")) %>%
gather(Group, Value) %>%
mutate(Group = gsub("_\d+$","",Group)) %>%
group_by(Group) %>%
summarise(Value = mean(Value, na.rm = TRUE))
# # A tibble: 3 x 2
# Group Value
# <chr> <dbl>
# 1 Group_1 5.50
# 2 Group_2 15.5
# 3 Group_3 25.5
数据:
Data is created to represent just `12` columns of the dataset mentioned by OP.
set.seed(1)
dataset <- data.frame(col1 = sample(1:10,10),
col2 = sample(1:10,10),
col3 = sample(1:10,10),
col4 = sample(1:10,10),
col5 = sample(11:20,10),
col6 = sample(11:20,10),
col7 = sample(11:20,10),
col8 = sample(11:20,10),
col9 = sample(21:30,10),
col10 = sample(21:30,10),
col11 = sample(21:30,10),
col12 = sample(21:30,10)
)
dataset
# col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12
# 1 3 3 10 5 19 15 20 14 25 23 27 30
# 2 4 2 2 6 16 18 13 18 27 21 24 27
# 3 5 6 6 4 17 14 14 13 24 26 23 23
# 4 7 10 1 2 14 12 19 19 23 27 30 24
# 5 2 5 9 10 18 11 18 17 30 25 29 21
# 6 8 7 8 8 20 16 12 15 22 24 22 26
# 7 9 8 7 9 11 17 15 20 29 22 21 28
# 8 6 4 5 1 12 19 17 12 21 28 25 25
# 9 10 1 3 7 13 13 11 16 28 30 28 29
# 10 1 9 4 3 15 20 16 11 26 29 26 22