[英]how to combine some columns?
I have 3 columns as below:我有3列如下:
col1 col2 col3
1 NA NA
NA 3 NA
NA NA NA
3 NA NA
how I can combine these 3 column and make a new one?我如何结合这 3 列并制作一个新列?
col1 col2 col3 new
1 NA NA 1
NA 3 NA 3
NA NA NA NA
3 NA NA 3
Notice they don't have intersection, meaning that if one of them is a number 2 others are NA请注意,它们没有交集,这意味着如果其中一个是数字 2,则其他是 NA
Let's say your dataframe is called df
,假设您的 dataframe 被称为df
,
df$new <- pmin(df$col1,df$col2,df$col3,na.rm=TRUE)
should answer your question.应该回答你的问题。
The pmin
function get the minimum of the three columns of each row, and the na.rm=TRUE
ignores the NA values, so if by row you only have at most one non NA value this should work. pmin
function 获得每行三列中的最小值,并且na.rm=TRUE
忽略 NA 值,因此如果按行您最多只有一个非 NA 值,这应该可以工作。
We can use max.col
to get the non-NA value in each row.我们可以使用max.col
来获取每行中的非 NA 值。
df$new <- df[cbind(seq_len(nrow(df)), max.col(!is.na(df)))]
df
# col1 col2 col3 new
#1 1 NA NA 1
#2 NA 3 NA 3
#3 NA NA NA NA
#4 3 NA NA 3
If you more than 1 value which is not not NA in a row you might want to look into ties.method
of max.col
based on your requirement.如果您连续超过 1 个不是 NA 的值,您可能需要根据您的要求查看ties.method
的max.col
。
We can also use coalesce
from dplyr
我们也可以使用coalesce
的dplyr
library(dplyr)
df1 %>%
mutate(new = coalesce(col1, col2, col3))
# col1 col2 col3 new
#1 1 NA NA 1
#2 NA 3 NA 3
#3 NA NA NA NA
#4 3 NA NA 3
or instead of specifying the column names或者不指定列名
df1 %>%
mutate(new = coalesce(!!! .))
Or with reduce
或reduce
library(purrr)
df1 %>%
mutate(new = reduce(., coalesce))
df1 <- structure(list(col1 = c(1L, NA, NA, 3L), col2 = c(NA, 3L, NA,
NA), col3 = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_
)), row.names = c(NA, -4L), class = "data.frame")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.