开发者

creating one new data frame applying one function over existing one

I'm trying to create a new data frame using an existing one with data in pairs

TargetID    A1           A2           B1      B2     
cg00000108 0.94483140 0.959417300 0.94427000 0.956393400 
cg00000292 0.83331720开发者_JAVA百科 0.836168900 0.75568530 0.869691000 
cg00001594 0.00000000 0.009319287 0.00318779 0.001852309 
cg00003298 0.01775547 0.034981820 0.03380106 0.116663900 
cg00003345 0.55442110 0.542106600 0.54762020 0.624028200 
cg00004055 0.10287610 0.107147500 0.09293073 0.106663000

The idea is to get one data frame with the results of the subtraction between pairs,so getting finallythree final columns

TargetID A1-A2 B1-B2

I tried to us apply but i have no enough programming skills in R to get how to say to the function where to begin the subtraction

Thanks in advance


How about simply:

with(d, data.frame(TargetID, A1-A2, B1-B2))

where d is your data frame.


Here's an approach using R's indexing capabilities and vectorized operations. Generate two sequences of indices that correspond to the columns you want to subtract. Here's an example:

#5 row by 10 column matrix
m <- matrix(rnorm(50), ncol = 10)

#sequence one for columns 1,3,5,7,9
s1 <- seq(1,ncol(m),2)
#sequence two for columns 2,4,6,8,10
s2 <- seq(2,ncol(m),2)

#Create new matrix
m2 <- m[, s1] - m[, s2]

#apply some column names
colnames(m2) <- paste("x", s1, s2, sep = "_")

Just realized I had asked nearly an identical question previously here: Operate on multiple columns at once

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜