r - lapply inside an lapply function -


having learnt loops bad im trying use lapply inside lapply. have series of sequentially numbered dataframes. in each 1 replace columns 5 , 8 letters depending on values

`if value <2 value changed "l" (for loss)`,  `if equals 2 value should "d"`  , if >2 should "g". 

my starting dataframe follows

structure(list(chromosome = structure(c(1l, 1l, 1l, 1l, 1l, 1l ), .label = c("1", "10", "11", "12", "13", "14", "15", "16",  "17", "18", "19", "2", "20", "21", "22", "3", "4", "5", "6",  "7", "8", "9", "x", "y"), class = "factor"), start = c(1l, 100000001l,  10000001l, 1000001l, 100500001l, 101000001l), ratio.x = c(1.32971,  0.990806, 0.991636, 1.01224, 1.00196, 1.00834), medianratio.x = c(1.32971,  1.00378, 0.988738, 0.979015, 1.00378, 1.00378), copynumber.x = c(3l,  2l, 2l, 1l, 2l, 1l), ratio.y = c(-1, 0.718527, 1.09204, -1, 1.07779,  1.41024), medianratio.y = c(-1, 1.07779, 0.814437, 0.814437,  1.07779, 1.07779), copynumber.y = c(2l, 2l, 2l, 2l, 2l, 2l)), .names = c("chromosome",  "start", "ratio.x", "medianratio.x", "copynumber.x", "ratio.y",  "medianratio.y", "copynumber.y"), row.names = c(na, 6l), class = "data.frame") 

the code using follows

lst <- mget(ls(pattern='total\\d+'))  lapply(lst, function(df) {   lapply(df, function(x){   #mark out diploid "d"   x[,5][x[,5] == "2"] <- "d"   x[,8][x[,8] == "2"] <- "d"   #deletions "l"   x[,5][x[,5] < 2 & x[,5] !="d"] <- "l"   x[,8][x[,8] < 2 & x[,8] !="d"] <- "l"   #gains "g"   x[,5][x[,5] > 2 & x[,5] !="l" & x[,5] !="d"] <- "g"   x[,8][x[,8] > 2 & x[,8] !="l" & x[,8] !="d"] <- "g"   #compare g's l's , d's } )}) 

however keep getting error

error in `[.default`(`*tmp*`, , 5) : incorrect number of dimensions 

here's alternative avoids looping (hidden or otherwise) altogether:

df; ##   chromosome     start  ratio.x medianratio.x copynumber.x   ratio.y medianratio.y copynumber.y ## 1          1         1 1.329710      1.329710            3 -1.000000     -1.000000            2 ## 2          1 100000001 0.990806      1.003780            2  0.718527      1.077790            2 ## 3          1  10000001 0.991636      0.988738            2  1.092040      0.814437            2 ## 4          1   1000001 1.012240      0.979015            1 -1.000000      0.814437            2 ## 5          1 100500001 1.001960      1.003780            2  1.077790      1.077790            2 ## 6          1 101000001 1.008340      1.003780            1  1.410240      1.077790            2 df[,c(5,8)] <- c('l','d','g')[sign(as.matrix(df[,c(5,8)])-2)+2]; df; ##   chromosome     start  ratio.x medianratio.x copynumber.x   ratio.y medianratio.y copynumber.y ## 1          1         1 1.329710      1.329710            g -1.000000     -1.000000            d ## 2          1 100000001 0.990806      1.003780            d  0.718527      1.077790            d ## 3          1  10000001 0.991636      0.988738            d  1.092040      0.814437            d ## 4          1   1000001 1.012240      0.979015            l -1.000000      0.814437            d ## 5          1 100500001 1.001960      1.003780            d  1.077790      1.077790            d ## 6          1 101000001 1.008340      1.003780            l  1.410240      1.077790            d 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -