Home > Mobile >  How can I replace certain values in a dataframe with the column name in r?
How can I replace certain values in a dataframe with the column name in r?

Time:09-28

I'm trying to replace certain character values in a dataframe with the variable name. For example, replacing all values of '1' with that value's corresponding variable name.

So I'd like to take a dataframe like this:

enter image description here

And end up with a dataframe like this:

enter image description here

I've written a for loop that works (please see below), but when I try to write the for loop as a function, the dataframe keeps the original values and I don't get any error messages. Any help to de-bug the function is appreciated! Here's a reproducible example.

# Create Dataframes ------------------------------------------------------------

testData1 <- data.frame(x = c('1', '0', '0', '0', '1'),
                        y = c('1', '1', '0', '1', '0'),
                        z = c('0', '0', '1', '0', '0'))

testData2 <- data.frame(x = c('1', '0', '0', '0', '1'),
                        y = c('1', '1', '0', '1', '0'),
                        z = c('0', '0', '1', '0', '0'))

# For Loop ---------------------------------------------------------------------

for (i in 1:nrow(testData1)) {
  for (j in 1:ncol(testData1)) {
    
    if (testData1[i, j] == '1') {
      
      testData1[i, j] <- colnames(testData1)[j]
      
    }
  }
}

# Function ---------------------------------------------------------------------

# takes a dataframe where all vectors are characters and replaces the value of 
# '1' with the name of the vector

replaceValues <- function(data) {
  
  for (i in 1:nrow(data)) {
    for (j in 1:ncol(data)) {
      
      if (data[i, j] == '1') {
        
        data[i, j] <- colnames(data)[j]

      }
    }
  }
}

replaceValues(testData2)

CodePudding user response:

We may do vectorized operations

i1 <- testData1 == 1
testData1[i1] <- names(testData1)[col(testData1)[i1]]

-output

> testData1
  x y z
1 x y 0
2 0 y 0
3 0 0 z
4 0 y 0
5 x 0 0

CodePudding user response:

via @akrun and @Dominik Żabiński

# Create Dataframes ------------------------------------------------------------

testData2 <- data.frame(x = c('1', '0', '0', '0', '1'),
                        y = c('1', '1', '0', '1', '0'),
                        z = c('0', '0', '1', '0', '0'))

# Function ---------------------------------------------------------------------

# takes a dataframe where all vectors are characters and replaces the value of 
# '1' with the name of the vector

replaceValues <- function(data) {
  
  for (i in 1:nrow(data)) {
    for (j in 1:ncol(data)) {
      
      if (data[i, j] == '1') {
        
        data[i, j] <- colnames(data)[j]

      }
    }
  }
  return(data)
}

testData2 <- replaceValues(testData2)
  • Related