简体   繁体   中英

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

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)

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

eg

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])

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)])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM