I have a data frame with one column identifying 4 participants and another column made of just one cell containing 88 observations -- below a reproducible example of the data frame:
pIndex <- c(1,2,3,4)
T1_AllSequence <- c("N11_d1p3_t0p4, N11_d1p3_t0p1, N11_d1p3_t0p3",
"N11_d0p1_t0p4, N11_d0p1_t0p7, N11_d0p1_t0p5",
"N7_d1p3_t0p4, N7_d1p3_t0p6, N7_d1p3_t0p4",
"N7_d0p5_t0p5, N7_d0p5_t0p6, N7_d0p5_t0p5")
Data <- as.data.frame(cbind(pIndex, T1_AllSequence))
dput(Data)
# structure(list(pIndex = structure(1:4, .Label = c("1", "2", "3",
# "4"), class = "factor"), T1_AllSequence = structure(c(2L, 1L,
# 4L, 3L), .Label = c("N11_d0p1_t0p4, N11_d0p1_t0p7, N11_d0p1_t0p5",
# "N11_d1p3_t0p4, N11_d1p3_t0p1, N11_d1p3_t0p3", "N7_d0p5_t0p5, N7_d0p5_t0p6, N7_d0p5_t0p5",
# "N7_d1p3_t0p4, N7_d1p3_t0p6, N7_d1p3_t0p4"), class = "factor")), class = "data.frame", row.names = c(NA,
# -4L))
I wrote a function to mutate the multiple observations contained in one cell of the column 'T1_AllSequence'into a long list.
#### Extracting variables from embedded data ####
i = T1$pIndex
Contours <- #for(i in 1:nrow(T1)){
function(i){
c = as.character(Data[i,'T1_AllSequence'])
Curvature <- as.data.frame(strsplit(c, ",")[[i]])
Curvature <- dplyr::rename (Curvature,
V1 = `strsplit(c, ",")[[i]]`)
Curvature <- mutate(Curvature,
pIndex = i,
order = as.integer(1:88),
vertex = ifelse(grepl("N7", V1), 7, 11),
distance = ifelse(grepl("d0p1", V1), 1,
ifelse(grepl("d0p5", V1), 5,
ifelse(grepl("d0p9", V1), 9, 13))),
tension = ifelse(grepl("t0p0", V1), 0,
ifelse(grepl("t0p1", V1), 1,
ifelse(grepl("t0p2", V1), 2,
ifelse(grepl("t0p3", V1), 3,
ifelse(grepl("t0p4", V1), 4,
ifelse(grepl("t0p5", V1), 5,
ifelse(grepl("t0p6", V1), 6,
ifelse(grepl("t0p7", V1), 7,
ifelse(grepl("t0p8", V1), 8,
ifelse(grepl("t0p9", V1), 9, 10)))))))))))
return(Curvature)
}
Now, I would like to apply my function to all the rows in my dataset, and then bind them together. So far, I tried multiple solutions, which I copy below:
require(plyr)
Stim <- ddply(T1, 1, Contours(T1))
Stim <- data.frame(t(apply(as.matrix(T1), 1, Contours)))
Stim <- apply(T1, 1, Contours())
Stim <- as.data.frame(apply(T1, 1, Contours))
Stim <- apply(T1[,442], 4, Contours)
Unfortunately, none of them works. The output I would like to obtain is something like this:
V1 PID order vertex distance tension
1 N11_d0p9_t0p0 1 1 11 9 0
2 N7_d1p3_t0p0 1 2 7 13 0
3 N11_d1p3_t0p3 1 3 11 13 3
4 N7_d0p5_t0p7 1 4 7 5 7
5 N7_d0p1_t0p1 1 5 7 1 1
6 N7_d0p9_t0p8 1 6 7 9 8
Here you can download the database to replicate the issue: T1database
Any suggestions on how I could do this would be much appreciated.