用向量划分数据框中的行

时间:2015-03-05 20:58:00

标签: r

我有这样的数据:

> dput(head(data))
structure(c(1L, 6L, 4L, 2L, 30L, 51L, 1L, 1L, 0L, 4L, 34L, 62L, 
0L, 5L, 2L, 2L, 16L, 69L, 2L, 2L, 2L, 1L, 18L, 113L, 0L, 4L, 
2L, 0L, 19L, 52L, 0L, 0L, 4L, 9L, 19L, 125L, 0L, 3L, 2L, 3L, 
24L, 50L, 0L, 10L, 17L, 22L, 6L, 82L, 0L, 12L, 22L, 6L, 29L, 
134L, 0L, 13L, 25L, 21L, 11L, 115L, 0L, 0L, 19L, 5L, 28L, 43L, 
0L, 8L, 33L, 5L, 11L, 134L, 3L, 18L, 28L, 18L, 7L, 168L, 0L, 
1L, 6L, 2L, 22L, 89L, 0L, 2L, 4L, 2L, 13L, 96L, 0L, 4L, 4L, 5L, 
15L, 73L, 0L, 3L, 15L, 6L, 16L, 56L, 0L, 0L, 2L, 0L, 7L, 51L, 
1L, 4L, 2L, 1L, 30L, 28L, 0L, 8L, 12L, 1L, 38L, 138L, 0L, 1L, 
0L, 0L, 4L, 47L, 0L, 1L, 11L, 13L, 23L, 70L, 0L, 2L, 2L, 0L, 
17L, 59L, 0L, 6L, 2L, 12L, 7L, 43L, 1L, 14L, 3L, 2L, 23L, 103L, 
0L, 13L, 19L, 21L, 8L, 94L, 0L, 2L, 0L, 9L, 13L, 116L, 0L, 1L, 
8L, 5L, 19L, 65L), .Dim = c(6L, 28L), .Dimnames = list(c("hsa-let-7a-5p_GTGAGGTAGTAGGTTGTAT", 
"hsa-let-7a-5p_GTGAGGTAGTAGGTTGTATAGTT", "hsa-let-7a-5p_TGAGGTAGTAGGTTGTATAGTTTA", 
"hsa-let-7a-5p_TGAGGTAGTAGGTTGTATAGTTTT", "hsa-let-7b-3p_CTATACAACCTACTGCCTTCC", 
"hsa-let-7b-5p_TGAGGTAGTAGGTTGTG"), c("X007_ATGTCA", "X026_GTAGAG", 
"X030_CGATGT", "X040_TGACCA", "X086_GTCCGC", "X087_ATTCCT", "X125_ACTGAT", 
"X197_GTTTCG", "X208_AGTTCC", "X226_TTAGGC", "X284_CGTACG", "X346_GGCTAC", 
"X387_ACAGTG", "X429_GGTAGC", "X471_AGTCAA", "X549_CAACTA", "X626_CAAAAG", 
"X659_ATCACG", "X705_CCGTCC", "X715_CACCGG", "X740_GAGTGG", "X760_GATCAG", 
"X785_CAGATC", "X809_GTGGCC", "X855_ACTTGA", "X859_CTTGTA", "X906_GCCAAT", 
"X996_GTGAAA")))

我想用这个向量划分data中的每一行:

> dput(head(spike))
structure(list(V1 = 17998L, V2 = 13354.5, V3 = 18987L, V4 = 24689.5, 
    V5 = 18585L, V6 = 17089L, V7 = 16554L, V8 = 13697L, V9 = 25024.5, 
    V10 = 20039L, V11 = 14630L, V12 = 19659L, V13 = 20782L, V14 = 14122.5, 
    V15 = 21882L, V16 = 12361.5, V17 = 13975L, V18 = 19046L, 
    V19 = 19089L, V20 = 16812L, V21 = 14353.5, V22 = 17018L, 
    V23 = 17838.5, V24 = 15826L, V25 = 21450L, V26 = 16304L, 
    V27 = 19139L, V28 = 18482L), .Names = c("V1", "V2", "V3", 
"V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13", 
"V14", "V15", "V16", "V17", "V18", "V19", "V20", "V21", "V22", 
"V23", "V24", "V25", "V26", "V27", "V28"), row.names = 1L, class = "data.frame")

我尝试了以下代码但没有成功:data[,2:28] <- sweep(data[,2:28],MARGIN=2,spike,"/")

2 个答案:

答案 0 :(得分:2)

spike填充为与data相同的维度,然后普通部门将起作用:

data / spike[rep(1, nrow(data)), ]

答案 1 :(得分:1)

尝试

sweep(data[,-1],MARGIN=2,unlist(spike[-1]),"/")