I have the following data frame,
c1 <- c(1,2,"<NA>","<NA>")
c2 <- c("<NA>","<NA>",3,4)
df <- data.frame(c1,c2)
>df
c1 c2
1 1 <NA>
2 2 <NA>
3 <NA> 3
4 <NA> 4
The following is the desired output that I'm trying to obtain after merging columns 1 ,2
>df
c1
1 1
2 2
3 3
4 4
I tried,
df <- mutate(df, x =paste(c1,c2))
which gives
> df
c1 c2 x
1 1 <NA> 1 <NA>
2 2 <NA> 2 <NA>
3 <NA> 3 <NA> 3
4 <NA> 4 <NA> 4
Could someone give suggestions on how to obtain the desired output?
One way is this:
c1 <- c(1, 2, NA, NA)
c2 <- c(NA, NA, 3, 4)
df <- data.frame(c1, c2)
df2 <- data.frame(
c1 = ifelse(is.na(df$c1), df$c2, df$c1)
)
#df2
# c1
#1 1
#2 2
#3 3
#4 4
You are close, but you are pasting together two strings where one uses the string NA in angled brackets to represent nothing, and if you are pasting strings together and want a string to not appear in the pasted string you need to have it as a zero length string. You can do this using the recode
command in dplyr
.
You can modify your code to be:
library(dplyr)
df <- mutate(df, x =paste0(recode(c1,"<NA>" = ""),recode(c2,"<NA>" = "")))
Another way using dplyr
from tidyverse
:
df2 <- df %>%
mutate(c3 = if_else(is.na(c1),c2,c1)) %>%
select(-c1, -c2) %>% # Given you only wanted one column
rename(c1 = c3) # Given you wanted the column to be called c1
Output:
c1
1 1
2 2
3 3
4 4
You could use rowSums
:
data.frame(c1 = rowSums(df,na.rm = TRUE))
# c1
# 1 1
# 2 2
# 3 3
# 4 4
Since it seems the the dataframe actually contains NA
values rather than '<NA>'
strings, I would suggest to use coalesce
:
c1 <- c(1,2,NA, NA)
c2 <- c(NA, NA,3,4)
df <- data.frame(c1,c2)
library(tidyverse)
df %>%
mutate(c3=coalesce(c1, c2))
Output:
c1 c2 c3
1 1 NA 1
2 2 NA 2
3 NA 3 3
4 NA 4 4
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.