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"))
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"))
(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