1

我有一个包含以下列名称和 19 行的数据框

> [1] "X12C.Signal_Grn"    "X12C.Signal_Red"    "X11P.Signal_Grn"   
> [4] "X11P.Signal_Red"    "X7P_.A..Signal_Grn" "X7P_.A..Signal_Red"
> [7] "X8P.Signal_Grn"     "X8P.Signal_Red"     "X9P.Signal_Grn"    
>[10] "X9P.Signal_Red"     "X10PA.Signal_Grn"   "X10PA.Signal_Red"  
>[13] "X6PB.Signal_Grn"    "X6PB.Signal_Red"    "X12P.Signal_Grn"   
>[16] "X12P.Signal_Red"    "X5P.Signal_Grn"     "X5P.Signal_Red"    
>[19] "X9P2C15.Signal_Grn" "X9P2C15.Signal_Red" "X42Pa.Signal_Grn"  
>[22] "X42Pa.Signal_Red"  

该类是data.frame。问题是如何为原始参考中的所有行创建一个新的数据框,其中包含第 1 列和第 2 列的 log 2 比率,然后是 3 和 4,然后是 5 和 6,依此类推?

4

1 回答 1

3

这个解决方案不是那么漂亮,但它可以满足您的需求。

# Generate some random data with column names on the same format as you have
df <- data.frame(matrix(runif(19*22), 19, 22))
names(df) <- sprintf("%s.Signal_%s", rep(LETTERS[1:11], each=2),
                                     rep(c("Grn", "Red"), 11))
# Calculate log2 ratios
l2r <- as.data.frame(lapply(1:(ncol(df)/2), function(i) log2(df[2*i]/df[2*i-1])))
# Give the columns comprehensive names
names(l2r) <- sub("Signal_Red", "log2ratio", names(df)[seq(2, ncol(df), 2)])
于 2012-09-28T15:18:32.810 回答