How to replace certain data frame value with it's unknown column name?

One way using base R

#Replace all the values with 1:3 with blank
df[-1][sapply(df[-1], `%in%`, 1:3)] <- ""
#Get the row/column indices where value is 4
mat <- which(df == 4, arr.ind = TRUE)
#Exclude values from first column
mat <- mat[mat[, 2] != 1, ]
#Replace remaining entries with it's corresponding column names
df[mat] <- names(df)[mat[, 2]]
df

#  id unknownvarname1 unknownvarname2
#1  1                 unknownvarname2
#2  2                                
#3  3                                
#4  4 unknownvarname1                
#5  5                 unknownvarname2
#6  6                                
#7  7                                
#8  8 unknownvarname1                

Just to give another option with switch (though, as this function is not vectorized, it needs a nested sapply within a lapply which doesn't make it that "pretty" and efficient...):

Basically, switch works with numeric as switch(myNumberToTest, caseIfOne, caseIfTwo, ...).

So what you need is:

df[, 2:3] <- lapply(2:3, function(x) sapply(df[, x], switch, "", "", "", names(df)[x]))

df
#  id unknownvarname1 unknownvarname2
#1  1                 unknownvarname2
#2  2                                
#3  3                                
#4  4 unknownvarname1                
#5  5                 unknownvarname2
#6  6                                
#7  7                                
#8  8 unknownvarname1                

Yet another base R option, using ifelse within lapply (still looping on the columns, but vectorized approach by column):

df <- data.frame(id=1:8,unknownvarname1=c(1:4,1:4),unknownvarname2=c(4:1,4:1))
df[,2:3] <- lapply(2:3, function(x) { ifelse(df[,x] < 4, "", colnames(df)[x]) })

gives

  id unknownvarname1 unknownvarname2
1  1                 unknownvarname2
2  2                                
3  3                                
4  4 unknownvarname1                
5  5                 unknownvarname2
6  6                                
7  7                                
8  8 unknownvarname1         

Tags:

R