Data wrangling: How to order every same value of a row under a column

Hi community,
I need to import data to a survey software and for that I have to transform my data into a specific required format.

My data looks like this:

data <- data.frame(
C1 = c("Red",NA,"Yellow",NA,NA),
C2 = c("Yellow",NA,NA,NA,"Red"),
C3 = c(NA,"Blue","Red","Blue","Green"),
C4 = c("Blue",NA,NA,"Green",NA),
C5 = c("Orange","Red",NA,"Yellow","Yellow"),
row.names = c("R1","R2","R3","R4","R5"))

data_frame1

And I need to order my row values in such a way that every coincident values remain under a column. This would be a desirable order:

data2 <- data.frame(
C1 = c("Yellow",NA,"Yellow","Yellow","Yellow"),
C2 = c("Red","Red","Red",NA,"Red"),
C3 = c("Blue","Blue",NA,"Blue",NA),
C4 = c(NA,NA,NA,"Green","Green"),
C5 = c("Orange",NA,NA,NA,NA),
row.names = c("R1","R2","R3","R4","R5"))

data_frame2
(It doesn't matter if the first or other column is yellow or red and so on...)

So I was trying to do it with apply

apply(data,1, function(x) {
  ifelse(any(x=="Yellow"),"Yellow",NA)
  })

But I did not succeed and I would like to avoid write every value's name (there are many and with really long names in my actual data).
Does anybody have an idea of what should I do?

thanks in advance

Is this close enough? It has the correct information, I think, but the labeling is different than you asked for.

data <- data.frame(
  C1 = c("Red",NA,"Yellow",NA,NA),
  C2 = c("Yellow",NA,NA,NA,"Red"),
  C3 = c(NA,"Blue","Red","Blue","Green"),
  C4 = c("Blue",NA,NA,"Green",NA),
  C5 = c("Orange","Red",NA,"Yellow","Yellow"),
  row.names = c("R1","R2","R3","R4","R5"), stringsAsFactors = FALSE)
data$Row = row.names(data)
data2 <- tidyr::gather(data, key = "col", value = "value", C1:C5)
data3 <- unique(data2)
data3 <- dplyr::filter(data3, !is.na(value))
data3$count <- 1
data3 <- dplyr::select(data3, -col)
data4 <- tidyr::spread(data3, key = "value", value = "count")
data4
#>   Row Blue Green Orange Red Yellow
#> 1  R1    1    NA      1   1      1
#> 2  R2    1    NA     NA   1     NA
#> 3  R3   NA    NA     NA   1      1
#> 4  R4    1     1     NA  NA      1
#> 5  R5   NA     1     NA   1      1

Created on 2020-02-07 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.