[英]combining two columns to one in R
V1<-c("Apple", "Orange", "NA", "NA")
V2<-c("NA", NA", "Strawberries", "NA")
V3<-data.frame(V1, V2)
Want to get the following: 想要得到以下内容:
V4<-c("Apple", "Orange", "Strawberries", "NA")
V4<-data.frame(V4)
Any suggestions? 有什么建议么? Thanks!!
谢谢!!
V1<-c("Apple", "Orange", "NA", "NA")
V2<-c("NA", "NA", "Strawberries", "NA")
V3<-data.frame(V1, V2)
V3$V4 <- with(V3, ifelse(V1 == "NA" & V2 != "NA", as.character(V2),
ifelse(V1 != "NA" & V2 == "NA", as.character(V1), "NA")))
This gives: 这给出:
V1 V2 V4
1 Apple NA Apple
2 Orange NA Orange
3 NA Strawberries Strawberries
4 NA NA NA
Using dplyr
's coalesce
, also reading in the strings not as factors: 使用
dplyr
的coalesce
,也不dplyr
字符串作为因素读取:
V1<-c("Apple", "Orange", NA, NA)
V2<-c(NA, NA, "Strawberries", NA)
V3 <-data.frame(V1, V2, stringsAsFactors = FALSE)
library(dplyr)
V3 %>% mutate(V4 = coalesce(V1, V2))
V1 V2 V4
1 Apple NA Apple
2 Orange <NA> Orange
3 <NA> Strawberries Strawberries
4 <NA> <NA> <NA>
There is also the possibility to use case_when
from dplyr. 也有使用的可能性
case_when
从dplyr。 I like it because it is much easier to keep track of what is happening compared to nested ifelse
clauses: 我喜欢它,因为与嵌套的
ifelse
子句相比,跟踪发生的事情要容易得多:
library(dplyr)
V1<-c("Apple", "Orange", "NA", "NA")
V2<-c("NA", "NA", "Strawberries", "NA")
V3<-data.frame(V1, V2)
V3 <- V3 %>%
mutate(V4 = case_when(V1 == "NA" & V2 != "NA" ~ as.character(V2),
V1 != "NA" & V2 == "NA" ~ as.character(V1),
TRUE ~ as.character("NA") ) )
In this case the difference is probably not so big, but the more conditions are tested, the easier it gets. 在这种情况下,差异可能不会太大,但是测试的条件越多,越容易获得。 If for instance you also need to treat the case where there is a non-"NA" value in both columns, this would simply extend to something like:
例如,如果您还需要处理两列中都存在非“ NA”值的情况,则可以将其简单地扩展为:
V1<-c("Apple", "Orange", "NA", "NA", "Banana")
V2<-c("NA", "NA", "Strawberries", "NA", "Pear")
V3<-data.frame(V1, V2)
V3 <- V3 %>%
mutate(V4 = case_when(V1 == "NA" & V2 != "NA" ~ as.character(V2),
V1 != "NA" & V2 == "NA" ~ as.character(V1),
V1 != "NA" & V2 != "NA" ~ paste(V1,V2),
TRUE ~ as.character("NA") ) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.