r - Convert comma separated entry to columns -


i have dataset several columns, 1 of column reaction times. these reaction times comma separated denote reaction times (of same participant) different trials.

for example: row 1 (i.e.: data participant 1) has following under column "reaction times"

reaction_times 2000,1450,1800,2200 

hence these reaction times of participant 1 trials 1,2,3,4.

i want create new data set in reaction times these trials form individual columns. way can calculate mean reaction time each trial.

              trial 1  trial 2  trial 3  trial 4  participant 1:   2000     1450     1800     2200 

i tried "colsplit" "reshape2"-package doesn't seem split data new columns (perhaps because data in 1 cell).

any suggestions?

i think looking strsplit() function;

a = "2000,1450,1800,2200" strsplit(a, ",") [[1]]                                                                                                                                                        [1] "2000" "1450" "1800" "2200"    

notice strsplit returns list, in case 1 element. because strsplit takes vectors input. therefore, can put long vector of single cell characters function , splitted list of vector. in more relevant example like:

# create example data dat = data.frame(reaction_time =         apply(matrix(round(runif(100, 1, 2000)),                       25, 4), 1, paste, collapse = ","),                      stringsasfactors=false) splitdat = do.call("rbind", strsplit(dat$reaction_time, ",")) splitdat = data.frame(apply(splitdat, 2, as.numeric)) names(splitdat) = paste("trial", 1:4, sep = "") head(splitdat)   trial1 trial2 trial3 trial4 1    597   1071   1430    997 2    614    322   1242   1140 3   1522   1679     51   1120 4    225   1988   1938   1068 5    621    623   1174     55 6   1918   1828    136   1816 

and finally, calculate mean per person:

apply(splitdat, 1, mean) [1] 1187.50  361.25  963.75 1017.00  916.25 1409.50  730.00 1310.75 1133.75 [10]  851.25  914.75  881.25  889.00 1014.75  676.75  850.50  805.00 1460.00 [19]  901.00 1443.50  507.25  691.50 1090.00  833.25  669.25 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -