I have a dataset like this (df1
)
ID 2 4 6 8 10 12 14 16 18 20 22 24 Day
1 0 0 0 0 2 0 0 0 1 0 1 0 Sunday
1 0 0 0 0 0 4 0 0 0 0 0 0 Monday
1 0 0 0 0 0 0 0 0 2 0 0 0 Tuesday
1 0 0 0 0 0 0 2 0 0 0 0 0 Wednesday
1 0 0 0 0 0 0 0 2 0 0 0 0 Thursday
1 0 0 0 0 0 0 0 0 2 0 0 0 Friday
1 0 0 0 0 0 0 0 0 0 2 0 0 Saturday
2 0 0 0 0 0 0 0 0 0 0 0 0 Sunday
2 0 0 0 0 0 1 0 0 0 0 0 0 Monday
2 0 0 0 0 0 0 1 0 0 0 1 0 Tuesday
2 0 0 0 0 0 0 0 1 0 0 0 0 Wednesday
2 0 0 0 0 0 0 0 0 1 0 0 0 Thursday
2 0 0 0 0 0 2 0 0 0 1 0 0 Friday
2 0 0 0 0 0 0 0 0 0 0 0 0 Saturday
3 0 0 0 0 0 0 0 0 0 0 0 0 Sunday
3 0 0 0 0 0 0 2 0 0 0 0 0 Monday
3 0 0 0 0 0 1 0 0 2 0 0 0 Tuesday
3 0 0 0 0 0 0 0 0 0 0 0 0 Wednesday
3 0 0 0 0 0 0 0 2 0 0 0 0 Thursday
3 0 0 0 0 0 0 0 0 0 0 0 0 Friday
3 0 0 0 0 0 0 2 0 0 0 0 0 Saturday
3 0 0 0 0 0 0 0 2 0 0 0 0 Sunday
and I have an ID
checklist like this:
ID
1
2
3
I want to convert the df1
into this kind of output:
ID Var1 Var2 Var3 Var4 Var5 ...... Var82 Var83 Var84
1 0 0 0 0 2 2 0 0
2
3
where Var1
represents 'Sunday 2' (in the first dataframe) and var84 represents the 'Saturday24'. I want to export my result as a .csv
file.
I am doing this by using a for loop (shown below) because there are too many ID's. However, the problem is these codes are running very slowly. Is there any faster way to get the same result?
library(dplyr)
library(reshape2)
for (i in ID_checklist$ID) {
x= filter(df1$ID %in% i)
x$Day = NULL
df.melted = melt(t(x[,-1]), id.vars = NULL)
myNewDF = data.frame(i, t(df.melted[,3]))
write.table(myNewDF,file="my12x7.csv", append=TRUE,sep=",",col.names=FALSE,row.names=FALSE)
}