Balachandar Kaliappan
Balachandar Kaliappan

Reputation: 153

Renaming row values with column name and then aggregating it into one column using R

I have a data.frame with row values as 0 and 1. Now I need to replace the 1 with column name. Later I need to aggregate them to one column (since there is not duplication in a single row).

The dataframe looks like this one

var1   var2    var3 
0      1       0
1      0       0
0      0       1

Expected output

var1   var2    var3 var
0      var2    0    var2
var1   0       0    var1
0      0       var3 var3

Upvotes: 1

Views: 518

Answers (3)

slava-kohut
slava-kohut

Reputation: 4233

Base R solution for an arbitrary data frame and target value:

df <- data.frame(
var1  = c(0,1,0),
var2 = c(1,0,0),
var3 = c(0,0,1))

# Task 1 
val <- 1 # value to subsitute
for (n in names(df)){
  df[[n]][df[[n]] == val] <- n
}

# Task 2
df$var <- apply(df, 2, function(x) x[grepl("var", x)])

Upvotes: 1

Jwolf
Jwolf

Reputation: 90

You can use a combination of ifelse statements to replace 1's with the column name.

e.g.

df$var1 = ifelse(df$var1 == 1, "var1", 0)

if you want to iterate over multiple columns, you can probably make it more replicable by using lapply and a list of column names, and then instead of having "var1" in the ifelse statement use names(df[x])

Upvotes: 1

Fnguyen
Fnguyen

Reputation: 1177

You should try gather() from the tidyr package.

Gather will create one column with the var-name and one with the value. Afterwards you can then filter to only value 1 and then drop the value-column.

library(dplyr) # for piping
library(tidyr) # for gather

df %>%
gather(var,value) %>% # the arguments simply name the output columns
filter(value == 1) %>%
select(-value)

Upvotes: 1

Related Questions