0

I'm using survey data that is coded in a weird way. The survey states, "Enter the codes for the Machinery you use on the following lines:"

1: Truck

2: Loader

3: Flatbed

4 - 9: etc.

Line 1:

Line 2:

...

Line 7:

The only way I can figure out how to recode the data into variables for each piece of Machinery is with these large ifelse statements

importpath <- "C:/"

mydata <- read.table(paste0(importpath,"/","Machinery.csv"), header=TRUE, 
                     sep=",", row.names="ID")

mydata$Truck <- ifelse(mydata$Line1 == 1, 1, 
                ifelse(mydata$Line2 == 1, 1,
                ifelse(mydata$Line3 == 1, 1,
                ifelse(mydata$Line4 == 1, 1,
                ifelse(mydata$Line5 == 1, 1,
                ifelse(mydata$Line6 == 1, 1, 
                ifelse(mydata$Line7 == 1, 1, 
                                    0)))))))

mydata$Loader <- ifelse(mydata$Line1 == 2, 1, 
                ifelse(mydata$Line2 == 2, 1,
                ifelse(mydata$Line3 == 2, 1,
                ifelse(mydata$Line4 == 2, 1,
                ifelse(mydata$Line5 == 2, 1,
                ifelse(mydata$Line6 == 2, 1, 
                ifelse(mydata$Line7 == 2, 1, 
                                    0)))))))

mydata$FlatBed <- ifelse(mydata$Line1 == 3, 1, 
                ifelse(mydata$Line2 == 3, 1,
                ifelse(mydata$Line3 == 3, 1,
                ifelse(mydata$Line4 == 3, 1,
                ifelse(mydata$Line5 == 3, 1,
                ifelse(mydata$Line6 == 3, 1, 
                ifelse(mydata$Line7 == 3, 1, 
                                    0)))))))
greg44289
  • 55
  • 6

1 Answers1

2

I really wish you took the time to create a reproducible example. It's best to give a real example of your data rather than us having to guess what's in the files you are reading. But i'll guess your test data looks something like this (with only three categories)

dd<-data.frame(Line1=c(3,1,2,0,1), 
    Line2=c(0,2,0,0,3),
    Line3=c(0,3,0,NA,1)
)

then you could do

dd$Truck <- mapply(`%in%`, 1, split(dd[, paste0("Line",1:3)], 1:nrow(dd)))
dd$Loader <- mapply(`%in%`, 2, split(dd[, paste0("Line",1:3)], 1:nrow(dd)))
dd$FlatBed <- mapply(`%in%`, 3, split(dd[, paste0("Line",1:3)], 1:nrow(dd)))

We we just check to see if 1, 2 or 3 is in any of the columns. This is robust to NA values. If you don't have any NA values and just have all zeros, you could do

dd$Truck <- apply(dd[, paste0("Line",1:3)]==1, 1, any)
dd$Loader <- apply(dd[, paste0("Line",1:3)]==2, 1, any)
dd$FlatBed <- apply(dd[, paste0("Line",1:3)]==3, 1, any)
Community
  • 1
  • 1
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Man ... that is not what I imagined the data looked like, but since I've been awarded several upvotes for reading minds in the past I guess I owe this one to you. – IRTFM Oct 10 '14 at 23:06