最近遇到的小问题,需要对每一行的特定列求和
文件实例
df <- data.table(sample = c("test1","test2","test3"), value1 = c(1,2,3),value2 = c(2,4,5),value3 = c(2,1,7), value4 = c(4,2,1), group = c("Disease","Healthy","Disease"))
推荐一个集成R包 叫tidyverse
里面集成了非常多有用的R包 非常便利
- 将特定列转换成行名
df %>%
column_to_rownames(., var = "sample")
- 对value1到value3求和, 求和的方法非常多,这里使用mutate(增加新列) rowSum(求和) 来自dyplr
df %>%
column_to_rownames(., var = "sample") %>%
mutate(rowsum = rowSums(.[1:4]))
- 有时候给个变量,也可以识别,而不像data.table还需要加with=False
n <- 3
df %>%
column_to_rownames(., var = "sample") %>%
mutate(rowsum = rowSums(.[1:n]))
Ref :
https://www.rdocumentation.org/packages/textshape/versions/1.7.1/topics/column_to_rownames
https://dplyr.tidyverse.org/reference/mutate.html