r - Add up values in list -
i'm working list contains following data:
>resultslist $`1` [1] "x" "0" "1" "1" "1" "5" $`2` [1] "x /// y" "0" "1" "1" "2" "3" $`3` [1] "x" "0" "1" "3" "2" "4" $`4` [1] "x /// z" "0" "1" "2" "2" "2" $`5` [1] "x" "0" "1" "3" "3" "4" $`6` [1] "x" "0" "0" "0" "1" "2" $`7` [1] "x" "0" "2" "2" "1" "4" $`8` [1] "x /// y" "0" "2" "2" "1" "2"
i add numbers per column , keep value in column 1 present in every row.
the output should this:
>mergeddata [1] "x" "0" "9" "14" "13" "26"
how can accomplish this?
edit here solution assumes column 1 strings in form "var /// var2 /// ...". first recover unique variables this:
resultslist <- list(c("x","0","1","1","1","5"), c("x /// y","0","1","1","2","3"), c("x","0","1","3","2","4"), c("x /// z","0","1","2","2","2"), c("x","0","1","3","3","4"), c("x","0","0","0","1","2"), c("x","0","2","2","1","4"), c("x /// y","0","2","2","1","2")) firstcolumn <- sapply(resultslist,"[[",1) listsofvariables <- c(strsplit(firstcolumn," /// ")) vector <- c() for(i in 1:length(listsofvariables)) { vector <- c(vector,listsofvariables[[i]]) } uniquevariables <- unique(vector) uniquevariables [1] "x" "y" "z"
next, find out of these variables contained in of individual rows:
matches <- sapply(1:length(uniquevariables), function(x,y) grep(uniquevariables[x],y), y=firstcolumn) variablesmatchingallrows <- uniquevariables[sapply(matches,"length")==length(resultslist)] variablesmatchingallrows [1] "x"
we paste variables (in case have more 1 variable matches rows):
variablesmatchingallrowstest <- c("x","y","z") paste(variablesmatchingallrowstest,collapse=" /// ") [1] "x /// y /// z"
we obtain final column 1 string , add column sums:
> finalstring <- paste(variablesmatchingallrows,collapse=" /// ") > c(finalstring,colsums("mode<-"(do.call(rbind, resultslist)[ , -1], "numeric"))) [1] "x" "0" "9" "14" "13" "26"
old answer
in example below first find unique string in column 1 has minimal stringsize , check if minimal string included in other strings. calculate columnsums on rows match. use data example:
> resultslist <- list(c("x","0","1","1","1","5"), + c("a b x /// y","0","1","1","2","3"), + c("x","0","1","3","2","4"), + c("a /// z","0","1","3","3","4"), + c("bd x","0","1","5","3","6")) > resultslist [[1]] [1] "x" "0" "1" "1" "1" "5" [[2]] [1] "a b x /// y" "0" "1" "1" "2" "3" [[3]] [1] "x" "0" "1" "3" "2" "4" [[4]] [1] "a /// z" "0" "1" "3" "3" "4" [[5]] [1] "bd x" "0" "1" "5" "3" "6"
first, find minimalstring
, corresponding row indices match minimalstring
:
firstcolumn <- sapply(resultslist,"[[",1) minimalstring <- unique(firstcolumn[nchar(firstcolumn)==min(nchar(firstcolumn))]) indices <- grep(minimalstring,firstcolumn) # grep on first element in minimalstring
we get:
> minimalstring [1] "x" > indices [1] 1 2 3 5
in other words, rows except row 4 match minimalstring. next add columnsums on matching rows this:
> c(minimalstring, as.character(apply(sapply(2:6,function(x,y,z) as.numeric(sapply(y,"[[",x)),y=resultslist)[indices,],2,sum))) [1] "x" "0" "4" "10" "8" "18"
we break down further clarity:
the inner sapply(y,"[[",x))
fetch elements of index x in list y , return them vector. y = resultslist
, x = 2:6
. note have convert characters numerics first:
> intermediateresult <- sapply(2:6,function(x,y,z) as.numeric(sapply(y,"[[",x)),y=resultslist) > intermediateresult [,1] [,2] [,3] [,4] [,5] [1,] 0 1 1 1 5 [2,] 0 1 1 2 3 [3,] 0 1 3 2 4 [4,] 0 1 3 3 4 [5,] 0 1 5 3 6
next, calculate columnsums of rows match indices
:
> sums <- apply(intermediateresult[indices,],2,sum) > sums [1] 0 4 10 8 18
finally, still have convert sums characters , add unique column 1 identifier in front. get:
> finalresult <- c(minimalstring,as.character(sums)) > finalresult [1] "x" "0" "4" "10" "8" "18"
for example following results:
> resultslist <- list(c("x","0","1","1","1","5"), + c("x /// y","0","1","1","2","3"), + c("x","0","1","3","2","4"), + c("x /// z","0","1","2","2","2"), + c("x","0","1","3","3","4"), + c("x","0","0","0","1","2"), + c("x","0","2","2","1","4"), + c("x // y","0","2","2","1","2")) > firstcolumn <- sapply(resultslist,"[[",1) > minimalstring <- unique(firstcolumn[nchar(firstcolumn)==min(nchar(firstcolumn))]) > indices <- grep(minimalstring,firstcolumn) # grep on first element in minimalstring > minimalstring [1] "x" > indices [1] 1 2 3 4 5 6 7 8 > c(minimalstring, as.character(apply(sapply(2:6,function(x,y,z) as.numeric(sapply(y,"[[",x)),y=resultslist)[indices,],2,sum))) [1] "x" "0" "9" "14" "13" "26"
Comments
Post a Comment