0
votes

sorry for the simple question but I have a data.frame (df1) of for example 200 rows and 69 columns with some NA and some values equal to 35 and another "data.frame" (df2) of 200 rows and 1 column without NA. I would like to sum the df2 to every column of df1 leaving NA when it is NA and 35 when the entry is 35 in df1. For example:

 df1    
Col1      Col2      Col3    
  5        35        21     
 NA        4         12      
 35        1         NA       
....      ....      ......

 df1    
Estimates     
  5     
  1      
  9       
 ...     

Desired output:

 df1    
Col1      Col2      Col3    
 10        35        26     
 NA        5         13      
 35        10        NA       
....      ....      ......

Can anyone help me please?

Thanks in advance

1
df1[] <- lapply(df1, `+`, df2$Estimates) or use matrices: data.frame(as.matrix(df1) + df2$Estimates) - alistaire
but in col2 you changed 35 to 40 (35+5)... - Sotos
Sorry for the mistake! - Bfu38

1 Answers

2
votes

One way to go,

#create logical matrix of df1 == 35
m1 <- df1 == 35

df1[] <- lapply(df1, function(i) i+df2$Estimates)
df1[m1] <- 35
df1
#  Col1 Col2 Col3
#1   10   35   26
#2   NA    5   13
#3   35   10   NA