0

I created a new column with values from another data frame. The problem is that there are empty rows in both data frames. So I assigned incorrectly the names first empty row with the second table for empty rows in first table.

My code:

df1 <- data.frame(age=c(23," ",55,34,45),
                  name=c("A","S","P","J","M"))


df2 <- data.frame(age=c(" ",43,55,34,45),
                  name=c("Alex","Silvia","Peter","Jack","Michael"))


df1$names2 <- df2$name[match(df1$age, df2$age)]

> df1
  age name  names2
1  23    A    <NA>
2        S    Alex
3  55    P   Peter
4  34    J    Jack
5  45    M Michael

First result record is ok, but the second shows incorrect information.

Zizou
  • 503
  • 5
  • 18

1 Answers1

1

This can be solved in multiple ways. Quick fix would be after you have done match replace corresponding names2 of empty values in age to NA.

df1$names2 <- df2$name[match(df1$age, df2$age)]
df1$names2[df1$age == " "] <- NA

df1
#  age name  names2
#1  23    A    <NA>
#2        S    <NA>
#3  55    P   Peter
#4  34    J    Jack
#5  45    M Michael

Or before doing match remove the empty values

inds <- df1$age != " "
df1$names2[inds] <- df2$name[match(df1$age[inds], df2$age)]

data

df1 <- data.frame(age=c(23," ",55,34,45),
          name=c("A","S","P","J","M"), stringsAsFactors = FALSE)

df2 <- data.frame(age=c(" ",43,55,34,45),
        name=c("Alex","Silvia","Peter","Jack","Michael"), stringsAsFactors = FALSE)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213