R中的单元均值 [英] Mean of Cell in R
问题描述
我的初始数据如下:
ID<-c(1,2,3,4)
Value<-c("1,2","0,-1",1,"","")
Data<-data.frame(ID, Value)
我想从每行的Value中创建一个MeanValue.如果值中没有值,我想取值的均值.
I want to create a MeanValue from Value for every Row. And if the Value is having no Value in it, i would like to take the Mean for the Value.
我计算第一步的想法是:
My Idea to Compute the Mean for the first Step was:
library(stringr)
AverageMean<-mean(as.numeric(str_split(Data$Value, ",")))
但是它正在创建错误
最终数据应类似于:
ID<-c(1,2,3,4)
Value<-c("1,2","0,-1",1,"","")
AverageMean<-c(1.5,-0.5,1,0.666,0.666)
FinalData<-data.frame(ID, Value, AverageMean)
推荐答案
基于该信息并处理您的代码,首先在相关列上执行str_split
,输出为list
.要获取单个列表元素的mean
,可以将lapply
与mean
结合使用.然后unlist
并将最后一个值Val[length(Val)]
替换为所有其他值的mean
,并根据上述内容创建一个新列AverageMean
.
Based on the info, and working on your code, first you do str_split
on the concerned column and the output is a list
. For getting the mean
for individual list elements, you can use lapply
with mean
. Then unlist
it and replace the last value Val[length(Val)]
with the mean
of all other values and create a new column AverageMean
based on the above.
Val <- unlist(lapply(str_split(Data$Value, ","),
function(x) mean(as.numeric(x), na.rm=TRUE)))
Val[length(Val)] <- mean(Val[-length(Val)], na.rm=TRUE)
Data$AverageMean <- Val
Data
# ID Value AverageMean
#1 1 1,2 1.5000000
#2 2 0,-1 -0.5000000
#3 3 1 1.0000000
#4 4 0.6666667
更新
如果您有多个missing values
,并希望将其替换为列的mean
,
Update
If you have multiple missing values
and want to replace that with the mean
of the column,
Data <- data.frame(ID=1:5, Value=c("1,2", "0,-1", 1, "", ""))
Val <- unlist(lapply(str_split(Data$Value, ","),
function(x) mean(as.numeric(x), na.rm=TRUE)))
以上步骤相同.用is.na
创建一个逻辑索引,并通过取消逻辑索引!is.na
将所有那些缺失的值替换为没有缺失的值的平均值.
The above steps are the same. Create a logical index with is.na
and replace all those missing values by the mean of values that are not missing by negating the logical index !is.na
.
Val[is.na(Val)] <- mean(Val[!is.na(Val)])
Data$AverageMean <- Val
Data
# ID Value AverageMean
#1 1 1,2 1.5000000
#2 2 0,-1 -0.5000000
#3 3 1 1.0000000
#4 4 0.6666667
#5 5 0.6666667
这篇关于R中的单元均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!