[英]Creating new column based on other columns in data frame in R
I have a data frame with three columns and four rows.我有一个三列四行的数据框。 I want to create a new column based on available columns such that new column gets the maximum value of the corresponding row (no matter if there is NA or not).
我想根据可用列创建一个新列,以便新列获取相应行的最大值(无论是否存在 NA)。 If all are NAs, the new column gets NA.
如果都是 NA,则新列将获得 NA。 enter image description here
在此处输入图像描述
Thanks.谢谢。
We could use pmax
with na.rm
specified as TRUE
(assuming it is a data.frame
object and the missing values are NA
)我们可以使用
pmax
并将na.rm
指定为TRUE
(假设它是一个data.frame
object 并且缺失值是NA
)
df1$new_column <- do.call(pmax, c(df1, na.rm = TRUE))
-output -输出
> df1
A B C new_column
1 98 NA NA 98
2 NA NA NA NA
3 98 100 NA 100
4 98 100 200 200
df1 <- data.frame(A = c(98, NA, 98, 98), B = c(NA, NA, 100, 100),
C = c(NA, NA, NA, 200))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.