Replicate element in a list of vector if length(vector)==1 in R -
i have simple list of vectors , replicate element of every vectors have length of 1.
mylist <- list(c(98, 102), c(175, 177), c(239, 240), c(146, 147, 168, 169 ), c(240, 242), c(363, 391), c(144, 146, 146), 197, 126, c(181, 192)) results <- lapply(mylist,function(x) if(length(x)==1) rep(x[1],each=2))
i in results expected replicates, how keep in results vectors of length >1 ? can't find correct way this. i'm pretty sure quite simple...
thanx helping
you can add else
statement leave elements more values
mylist <- list(c(98, 102), c(175, 177), c(239, 240), c(146, 147, 168, 169 ), c(240, 242), c(363, 391), c(144, 146, 146), 197, 126, c(181, 192)) results <- lapply(mylist,function(x) if(length(x)==1) rep(x[1],each=2) else x)
which results in
[[1]] [1] 98 102 [[2]] [1] 175 177 [[3]] [1] 239 240 [[4]] [1] 146 147 168 169 [[5]] [1] 240 242 [[6]] [1] 363 391 [[7]] [1] 144 146 146 [[8]] [1] 197 197 [[9]] [1] 126 126 [[10]] [1] 181 192
Comments
Post a Comment