Chirayu Chamoli
Chirayu Chamoli

Reputation: 2076

How do i replace NA with values from other table which matches the heading of the table to a variable

The code looks like this, this works though but with a warning. warning is number of items to replace is not a multiple of replacement length, so i understand it has something to do with length but im not quite getting it.

df$var1[is.na(df.t$var1)]=avg[as.character(df.t$var2)]

df is the data frame containing var1 and var2, var1 is numeric and var2 is a factor. i want to replace NA from var1 in df with the value in array 'avg' whose names match the var2 values. i hope the question is clear! Also as.character does not really make a difference here.

first 4 lines of the data df looks like

structure(list(df.t.var1 = c(0L, 
0L, 0L, 0L), df.t.var2 = structure(c(14L, 
18L, 12L, 15L), .Label = c("AA", "AB", "AC", "AD", "AE", "BA", 
"BB", "BC", "BD", "BE", "CA", "CB", "CC", "CD", "CE", "DA", "DB", 
"DC", "DD", "DE", "EA", "EB", "EC", "ED", "EE", "FA", "FB", "FC", 
"FD", "FE", "GA", "GB", "GC", "GD", "GE"), class = "factor")), .Names = c("df.t.var1", 
"var2"), row.names = c(NA, 4L), class = "data.frame")

and the first 4 lines of 'avg' looks like

structure(c(0.0194610778443114, 0.0387323943661972, 0.035645472061657, 
0.0533656761673742), .Dim = 4L, .Dimnames = list(c("AA", "AB", 
"AC", "AD")))

Upvotes: 0

Views: 56

Answers (1)

Pierre L
Pierre L

Reputation: 28441

If you have the data frame df:

df <- data.frame(var1=c('AA', 'AA', 'AA', 'AC', 'AC', 'AD', 'AB'), 
                 var2=c(10, NA, 3,NA, 5, NA, 2))
df
  var1 var2
1   AA   10
2   AA   NA
3   AA    3
4   AC   NA
5   AC    5
6   AD   NA
7   AB    2

You can replace the NA values with the values from avg with:

df$var2[is.na(df$var2)] <- avg[match(df$var1[is.na(df$var2)], names(avg))]
df
#   var1        var2
# 1   AA 10.00000000
# 2   AA  0.01946108
# 3   AA  3.00000000
# 4   AC  0.03564547
# 5   AC  5.00000000
# 6   AD  0.05336568
# 7   AB  2.00000000

Upvotes: 1

Related Questions