简体   繁体   English

如何根据每行中一些变量的单独平均值创建一个新变量?

[英]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 中执行以下操作:

  • Create a new variable called 'Z' and bind it to my dataframe创建一个名为“Z”的新变量并将其绑定到我的 dataframe
  • This should be the mean of each row这应该是每一行的平均值
  • If some variable is blank or contains an 'NA' then it should not get counted for the mean.如果某个变量为空白或包含“NA”,则不应将其计算为平均值。

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.

相关问题 R:如何循环从数据框中选择基于名称的变量,并为每个变量创建一个包含第一个列均值的新变量? - R: How to loop over a name-based selection of variables from a dataframe and for each create a new variable containing the column mean of the first? 如何根据其他变量中的值为每个人创建二进制变量? - How to create binary variable for each individual based on value in other variable? 如何创建一个新变量,它是其他 2 个变量的平均值? - How to create a new variable which is the mean of 2 other variables? dplyr中的mutate_each:使用其他具有相同文本的列的平均行值创建新列 - mutate_each in dplyr: create new column with the mean row values of other columns with some text in common 当每行代表一个人时,基于来自后续遭遇的信息的新列 - New column based on information from subsequent encounters when each row represents an individual 创建新变量,将数据帧的所有变量除以每一行中的相同变量-R - Create new variables dividing all variables of a data frame by the same variable in each row - R 如何根据状态级数据框中的值在单个级数据框中创建新变量? - how to create a new variable in an individual-level data frame based on values from a state-level data frame? 根据其他变量的连续观察条件创建一个新变量 - Create a new variable based on condition of consecutive observations from other variables 如何在每一行上应用库命令并创建新变量 - How to apply a library command on each row and create a new variable 根据变量从下面的行创建新列 - Create new column from row below, based on variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM