在 R 中包含多个带有 NA 的列的数据帧的按行统计
Rowwise statistics of a dataframe containing several columns with NAs in R
问题概述
你好 R 专家,
我将感谢您对解决问题的支持 this.I 我正在尝试计算大型数据框的行计算 -15k 行,700 列,其中包括 NA。
我想计算以下内容来表示新的 columns:Min,Max,Mean,Median,Standard deviaton,variance,
第 10 个百分位数、第 30 个百分位数、第 70 个百分位数、第 90 个百分位数
在有 NA 的地方,计算应该跳过它们。在 dplyr 的 rowsie 命令中使用 na.rm = True 没有成功。
加载 Dataframe 子集的代码
#Please note that the real dataframe has hundreds of columns, so typing each column won't be possible
df<- data.frame(a1=c(1,NA,0,4), a2=c(NA,1,0,6), a3=c(NA,NA,9,3),a4=c(1,NA,NA,4), a5=c(4,NA,NA,6), a6=c(7,NA,9,3),a7=c(1,1,1,1),a8=c(2,2,2,2), a9=c(4,3,3,6), a10=c(7,4,9,3))
df
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
1 1 NA NA 1 4 7 1 2 4 7
2 NA 1 NA NA NA NA 1 2 3 4
3 0 0 9 NA NA 9 1 2 3 9
4 4 6 3 4 6 3 1 2 6 3
预期输出
我想获取上述每一行的统计信息。尽管使用了 argumemnt "na.rm = True"
,但由于 NA,在使用 dplyr 的行向计算时出现错误
df
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 min mean median max sd variance per10 per30 per70 per90
1 1 NA NA 1 4 7 1 2 4 7 x x x x x x x x x x
2 NA 1 NA NA NA NA 1 2 3 4 x x x x x x x x x x
3 0 0 9 NA NA 9 1 2 3 9 x x x x x x x x x x
4 4 6 3 4 6 3 1 2 6 3 x x x x x x x x x x
期待您的帮助和 R 社区的发展
df$min<-apply(df,1,min,na.rm=TRUE)
df$mean<-apply(df,1,mean,na.rm=TRUE)
df$median<-apply(df,1,median,na.rm=TRUE)
df$max<-apply(df,1,max,na.rm=TRUE)
df$sd<-apply(df,1,sd,na.rm=TRUE)
df$variance<-apply(df,1,var,na.rm=TRUE)
df$per10<-apply(df,1,quantile,probs=0.1,na.rm=TRUE)
df$per30<-apply(df,1,quantile,probs=0.3,na.rm=TRUE)
df$per70<-apply(df,1,quantile,probs=0.7,na.rm=TRUE)
df$per90<-apply(df,1,quantile,probs=0.9,na.rm=TRUE)
当然你可以用 "eval(parse..)" 迭代函数的向量以获得相同的结果和更少的代码
我只包含了均值和 sd columns.You 其他参数也可以这样做。
library(dplyr)
df %>% mutate(mean = apply(df, MARGIN = 1, FUN = mean, na.rm = TRUE),
sd = apply(df, MARGIN = 1, FUN = sd, na.rm = TRUE))
此问题属于常见问题集'How do I calculate this/these statistics rowwise on (a selection of) columns in a data.frame'。
我想我会添加一个 base 和 dplyr 可扩展的通用方法,它可以处理任意数量的统计数据,并允许您 select 您想要汇总哪些列。请注意需要唯一的行标识符。
library(dplyr, quietly = TRUE, warn.conflicts = FALSE)
df <- data.frame(
a1=c(1,NA,0,4), a2=c(NA,1,0,6), a3=c(NA,NA,9,3),a4=c(1,NA,NA,4), a5=c(4,NA,NA,6), a6=c(7,NA,9,3),a7=c(1,1,1,1),a8=c(2,2,2,2), a9=c(4,3,3,6), a10=c(7,4,9,3))
# Base approach
funs_base = function(x) {
# Exclude missing values
x <- na.omit(unlist(x))
# Add desire functions to this vector
c(
mean = mean(x),
median = median(x),
sd = sd(x)
)
}
cbind(df, do.call(rbind, apply(
df, 1, function(x)
aggregate(x ~ 1, FUN = funs_base)[,1]
, simplify = FALSE)))
#> a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 mean median sd
#> 1 1 NA NA 1 4 7 1 2 4 7 3.375 3.0 2.559994
#> 2 NA 1 NA NA NA NA 1 2 3 4 2.200 2.0 1.303840
#> 3 0 0 9 NA NA 9 1 2 3 9 4.125 2.5 4.155461
#> 4 4 6 3 4 6 3 1 2 6 3 3.800 3.5 1.751190
# Dplyr approach
funs_dplyr = function(x) {
# Exclude missing values
x <- na.omit(unlist(x))
# Add desire functions to this dataframe
data.frame(
mean = mean(x),
median = median(x),
sd = sd(x)
)
}
df %>%
group_by(id = row_number()) %>%
tidyr::nest() %>% # Can choose columns to summarise here
mutate(stats = purrr::map(data, ~ funs_dplyr(.))) %>% # Calculate summaries
tidyr::unnest(cols = c(data, stats))
#> # A tibble: 4 x 14
#> # Groups: id [4]
#> id a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 mean median
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 NA NA 1 4 7 1 2 4 7 3.38 3
#> 2 2 NA 1 NA NA NA NA 1 2 3 4 2.2 2
#> 3 3 0 0 9 NA NA 9 1 2 3 9 4.12 2.5
#> 4 4 4 6 3 4 6 3 1 2 6 3 3.8 3.5
#> # ... with 1 more variable: sd <dbl>
由 reprex package (v2.0.1)
创建于 2022-02-16
问题概述
你好 R 专家,
我将感谢您对解决问题的支持 this.I 我正在尝试计算大型数据框的行计算 -15k 行,700 列,其中包括 NA。 我想计算以下内容来表示新的 columns:Min,Max,Mean,Median,Standard deviaton,variance, 第 10 个百分位数、第 30 个百分位数、第 70 个百分位数、第 90 个百分位数
在有 NA 的地方,计算应该跳过它们。在 dplyr 的 rowsie 命令中使用 na.rm = True 没有成功。
加载 Dataframe 子集的代码
#Please note that the real dataframe has hundreds of columns, so typing each column won't be possible
df<- data.frame(a1=c(1,NA,0,4), a2=c(NA,1,0,6), a3=c(NA,NA,9,3),a4=c(1,NA,NA,4), a5=c(4,NA,NA,6), a6=c(7,NA,9,3),a7=c(1,1,1,1),a8=c(2,2,2,2), a9=c(4,3,3,6), a10=c(7,4,9,3))
df
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
1 1 NA NA 1 4 7 1 2 4 7
2 NA 1 NA NA NA NA 1 2 3 4
3 0 0 9 NA NA 9 1 2 3 9
4 4 6 3 4 6 3 1 2 6 3
预期输出
我想获取上述每一行的统计信息。尽管使用了 argumemnt "na.rm = True"
,但由于 NA,在使用 dplyr 的行向计算时出现错误df
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 min mean median max sd variance per10 per30 per70 per90
1 1 NA NA 1 4 7 1 2 4 7 x x x x x x x x x x
2 NA 1 NA NA NA NA 1 2 3 4 x x x x x x x x x x
3 0 0 9 NA NA 9 1 2 3 9 x x x x x x x x x x
4 4 6 3 4 6 3 1 2 6 3 x x x x x x x x x x
期待您的帮助和 R 社区的发展
df$min<-apply(df,1,min,na.rm=TRUE)
df$mean<-apply(df,1,mean,na.rm=TRUE)
df$median<-apply(df,1,median,na.rm=TRUE)
df$max<-apply(df,1,max,na.rm=TRUE)
df$sd<-apply(df,1,sd,na.rm=TRUE)
df$variance<-apply(df,1,var,na.rm=TRUE)
df$per10<-apply(df,1,quantile,probs=0.1,na.rm=TRUE)
df$per30<-apply(df,1,quantile,probs=0.3,na.rm=TRUE)
df$per70<-apply(df,1,quantile,probs=0.7,na.rm=TRUE)
df$per90<-apply(df,1,quantile,probs=0.9,na.rm=TRUE)
当然你可以用 "eval(parse..)" 迭代函数的向量以获得相同的结果和更少的代码
我只包含了均值和 sd columns.You 其他参数也可以这样做。
library(dplyr)
df %>% mutate(mean = apply(df, MARGIN = 1, FUN = mean, na.rm = TRUE),
sd = apply(df, MARGIN = 1, FUN = sd, na.rm = TRUE))
此问题属于常见问题集'How do I calculate this/these statistics rowwise on (a selection of) columns in a data.frame'。
我想我会添加一个 base 和 dplyr 可扩展的通用方法,它可以处理任意数量的统计数据,并允许您 select 您想要汇总哪些列。请注意需要唯一的行标识符。
library(dplyr, quietly = TRUE, warn.conflicts = FALSE)
df <- data.frame(
a1=c(1,NA,0,4), a2=c(NA,1,0,6), a3=c(NA,NA,9,3),a4=c(1,NA,NA,4), a5=c(4,NA,NA,6), a6=c(7,NA,9,3),a7=c(1,1,1,1),a8=c(2,2,2,2), a9=c(4,3,3,6), a10=c(7,4,9,3))
# Base approach
funs_base = function(x) {
# Exclude missing values
x <- na.omit(unlist(x))
# Add desire functions to this vector
c(
mean = mean(x),
median = median(x),
sd = sd(x)
)
}
cbind(df, do.call(rbind, apply(
df, 1, function(x)
aggregate(x ~ 1, FUN = funs_base)[,1]
, simplify = FALSE)))
#> a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 mean median sd
#> 1 1 NA NA 1 4 7 1 2 4 7 3.375 3.0 2.559994
#> 2 NA 1 NA NA NA NA 1 2 3 4 2.200 2.0 1.303840
#> 3 0 0 9 NA NA 9 1 2 3 9 4.125 2.5 4.155461
#> 4 4 6 3 4 6 3 1 2 6 3 3.800 3.5 1.751190
# Dplyr approach
funs_dplyr = function(x) {
# Exclude missing values
x <- na.omit(unlist(x))
# Add desire functions to this dataframe
data.frame(
mean = mean(x),
median = median(x),
sd = sd(x)
)
}
df %>%
group_by(id = row_number()) %>%
tidyr::nest() %>% # Can choose columns to summarise here
mutate(stats = purrr::map(data, ~ funs_dplyr(.))) %>% # Calculate summaries
tidyr::unnest(cols = c(data, stats))
#> # A tibble: 4 x 14
#> # Groups: id [4]
#> id a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 mean median
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 NA NA 1 4 7 1 2 4 7 3.38 3
#> 2 2 NA 1 NA NA NA NA 1 2 3 4 2.2 2
#> 3 3 0 0 9 NA NA 9 1 2 3 9 4.12 2.5
#> 4 4 4 6 3 4 6 3 1 2 6 3 3.8 3.5
#> # ... with 1 more variable: sd <dbl>
由 reprex package (v2.0.1)
创建于 2022-02-16