r - Divide only one column of nested data.frames by an integer value -


i wish divide third column of dataframe 5. these dataframes nested , this:

[[44]]     ethnicity      variant  sum  1:       asw     acceptor    1  2:       asw          cds   68  3:       asw   cga_cnvwin 1000  4:       asw     cga_mirb    0  5:       asw       delete    0  6:       asw      disrupt    0  7:       asw        donor    0  8:       asw   frameshift    0  9:       asw       insert    1 10:       asw       intron   54 

i have used 3 commands each of successful has off-target effects.

lapply(aswldtsum,function(x)(x/5)) 

returns

[[44]]     ethnicity variant   sum  1:        na      na   0.2  2:        na      na  13.6  3:        na      na 200.0  4:        na      na   0.0  5:        na      na   0.0 

which has unfortunate effect of dividing rows 5, leading issues when class not integer in $sum column.

lapply(aswldtsum,function(x[,3])(x/5)) 

has effect of returning single vector, work nicely if not nested array of dataframes, statement

aswdtsum$newcol<-lapply(aswldtsum,function(x[,3])(x/5)) 

cannot written because nested.

using rapply in following statement:

rapply(aswldtsum,function(x) if (is.integer(x)) {(x/5)}) 

leads disordering of results.

so, there simple way either append 4th column each nested dataframe, or replace third column of each df (sum) value divided 5?

it simple, if aswldtsum name of list containing data frames, can do:

lapply(aswldtsum,fun=function(x) { x[,3]=x[,3]/5; return(x) }) 

basically replacing (entire) third column division of (entire) third colum five.

in practice:

> aswldtsum1=data.frame(ethnicity=rep('asw',10),variant=c("acceptor","cds","cga_cnvwin","cga_mirb","delete","disrupt","donor","frameshift","insert","intron"), sum=c(1,68,1000,0,0,0,0,0,1,54)) > #created first data.frame (equal example) > aswldtsum2=data.frame(ethnicity=rep('asw',10),variant=c("acceptor","cds","cga_cnvwin","cga_mirb","delete","disrupt","donor","frameshift","insert","intron"), sum=c(1,2,3,4,5,6,7,8,9,10)) > #created second data.frame (with different values third column) > aswldtsum=list(aswldtsum1,aswldtsum2) > #created list of data frames > lapply(aswldtsum,fun=function(x) { x[,3]=x[,3]/5; return(x) }) > #apply function divide third column each nested data.frame [[1]]    ethnicity    variant   sum 1        asw   acceptor   0.2 2        asw        cds  13.6 3        asw cga_cnvwin 200.0 4        asw   cga_mirb   0.0 5        asw     delete   0.0 6        asw    disrupt   0.0 7        asw      donor   0.0 8        asw frameshift   0.0 9        asw     insert   0.2 10       asw     intron  10.8  [[2]]    ethnicity    variant sum 1        asw   acceptor 0.2 2        asw        cds 0.4 3        asw cga_cnvwin 0.6 4        asw   cga_mirb 0.8 5        asw     delete 1.0 6        asw    disrupt 1.2 7        asw      donor 1.4 8        asw frameshift 1.6 9        asw     insert 1.8 10       asw     intron 2.0 > #desired result 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -