假设我的表看起来像这样:
> dat[1:5,1:5]
year terit A T C
1 2011 TA 24 93 126
2 2011 TB 15 46 57
3 2011 TC 0 0 0
4 2011 TD 0 54 287
5 2011 TE 36 31 129
我如何获得载体:
> TA2011
[1] AAAAAAAA(times=24)......TTTTTT(times=93)....etc.
答案 0 :(得分:2)
apply(dat[3:5], 1, function(x) paste(rep.int(names(dat)[3:5], x), collapse = ""))
会做到这一点。这将返回一个长字符串向量。
您的示例的结果:
1
"AAAAAAAAAAAAAAAAAAAAAAAATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
2
"AAAAAAAAAAAAAAATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
3
""
4
"TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
5
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
答案 1 :(得分:1)
啊,我看到@SvenHohenstein已经发布了apply
逻辑。我还添加了你想要相应命名字符串的部分
namestoassign <- paste(dat$terit,dat$year, sep = '')
for ( i in seq(paste(dat$terit,dat$year, sep = '')))
{
assign(
namestoassign[i],
apply(
dat[i,3:5],
1,
function(x)
paste(rep.int(names(dat)[3:5], x), collapse = "")
)
)
}