imprela - 2 months ago 44

R Question

I have a variable called "attendance" that that tracks daily attendance of a person. If it contains an "a" then the person attended the first day, "b" if the person attended the second day, all the way til "e" if the person attended the fifth day. So some people's attendance looks like "abc" or "cde" or "ace" if they attended first three days, last three days, or first/third/last days, respectively.

Now I want to create a variable "day_1" that equals 1 if "attendance" contains "a" and 0 otherwise. Then a variable "day_2" that equals 2 if "attendance" contains "b" and 0 otherwise. And so on until "day_5"

I have tried using the grep function to do it manually. Here is what I have tried so far:

`test <- data.frame(hhid = c(1:5),attendance=c("abc","bcd","d","ec","ade"))`

test$day_1 <- 0

test$day_2 <- 0

test$day_3 <- 0

test$day_1[grep("a", test$attendance, "day_1")] <- "1"

test$day_2[grep("b", test$attendance, "day_2")] <- "1"

test$day_3[grep("c", test$attendance, "day_3")] <- "1"

Is there a faster way of doing it in R?

Thanks a lot!

Answer

Since you haven't shared any sample data to work on, I created a sample vector to give you an idea.

```
test <- data.frame(hhid = c(1:5),attendance=c("abc","bcd","d","ec","ade"))
b = as.list(letters[1:5])
abc <- function(i){
ifelse(grepl(b[[i]], test$attendance),1,0)
}
m=sapply(seq_along(b), abc)
colnames(m)=paste("day",1:5,sep="_")
test <- cbind(test,m)
```