【发布时间】:2019-01-21 20:15:41
【问题描述】:
我有一个数据框:
df = read.table(text="race Chr1 Chr08 Chr11 rep1 rep2 rep3 rep4 rep5
race1 P54 P88 P54 151 142 267 127 161
race1 P54 P88 P88 131 203 120 300 223
race1 P54 P54 P88 165 271 73 170 241
race1 P54 P54 P54 206 235 76 67 159", header=T, stringsAsFactors=F)
我想添加列名作为列 2:4 的前缀,我尝试在循环中“粘贴”或应用。要么不行。
for (i in 2:4){
df[i] <- paste(names(df[i]),df[i],sep=".")}
或
df[2:4] <- apply(df[2:4],2, function(x) paste(colnames(x),x, sep="."))
预期结果:
result = read.table(text="race Chr1 Chr08 Chr11 rep rep2 rep3 rep4 rep5
race1 Chr1.P54 Chr08.P88 Chr11.P54 151 142 267 127 161
race1 Chr1.P54 Chr08.P88 Chr11.P88 131 203 120 300 223
race1 Chr1.P54 Chr08.P54 Chr11.P88 165 271 73 170 241
race1 Chr1.P54 Chr08.P54 Chr11.P54 206 235 76 67 159", header=T, stringsAsFactors=F)
感谢您的帮助。
【问题讨论】: