[英]How to create a new variable based on the individual mean of some variables from each row?
I have the following dataframe:我有以下 dataframe:
I want to the following in R:我想在 R 中执行以下操作:
Here is what my desired output would look like:这是我想要的 output 的样子:
Desired output in R: R 中所需的 output:
Here is what I have tried doing:这是我尝试做的事情:
I have tried to create a for loop which loops through each row to count number of non-blank variables, then it sums up all 3 variables and divides by the number of non-blank variables.我试图创建一个 for 循环,该循环遍历每一行以计算非空白变量的数量,然后将所有 3 个变量相加并除以非空白变量的数量。 Th problem is, I have to do this same task for 9 variables (in a dataframe which contains 50 variables) and that would mean a repetitive code and if blocks for 9 variables.
问题是,我必须对 9 个变量执行相同的任务(在包含 50 个变量的 dataframe 中),这意味着重复代码和 9 个变量的 if 块。
Is there a way around it?有办法解决吗?
The apply
function in base R allows to loop over rows of data.frames.基础 R 中的
apply
function 允许遍历 data.frames 行。 See ?apply
for more info.请参阅
?apply
以获取更多信息。 The mean
function has an na.rm
option to automatically deal with NA
entries. mean
function 有一个na.rm
选项来自动处理NA
条目。
x <- data.frame(
id = c("A", "B"),
A = c(5, 2),
B = c(NA, 5),
C = c(NA, 2)
)
x$Z <- apply(x[2:4], 1, function(x) { mean(x, na.rm = TRUE) })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.