Home > Software design >  Merge variables with different rows in R
Merge variables with different rows in R

Time:06-27

a = data.frame(c("Joe","Bill","Frank"), c("30","31",""))
b = data.frame(c("Frank","Chuck"), c("32","40"))
names (a) = c("name","age")
names (b) = c("name","age")

How can I merge a, b to get:

Joe 30 Bill 31 Frank 32 Chuck 40

CodePudding user response:

Option using full_join and remove row with blank cell:

library(dplyr)
a %>%
  full_join(., b) %>%
  na_if("") %>%
  na.omit()

Output:

Joining, by = c("name", "age")
   name age
1   Joe  30
2  Bill  31
4 Frank  32
5 Chuck  40

CodePudding user response:

Another possible solution:

library(dplyr)

bind_rows(a, b) %>% filter(age != "")

#>    name age
#> 1   Joe  30
#> 2  Bill  31
#> 3 Frank  32
#> 4 Chuck  40

CodePudding user response:

Try this

subset(rbind(a,b) , age != "")

output

   name age
1   Joe  30
2  Bill  31
4 Frank  32
5 Chuck  40
  •  Tags:  
  • r
  • Related