R中的rownames():为什么不使用paste0()?

时间:2017-10-05 15:00:22

标签: r matrix

我想知道为什么rownames()在我的下面的R代码中无法正常工作?

revenue = c(15,18,22,23,24, 22,25,15,15,14, 18,22,15,19,21, 
            23,15,14,17,18, 23,15,26,18,14, 12,15,11,10,8, 26,12,23,15,18, 
            19,17,15,20,10, 15,14,18,19,20, 14,18,10,12,23, 14,22,19,17,11, 
            21,23,11,18,14)

months = gl(12, 5)

years = gl(5, 1, length(revenue))

m = matrix(c(revenue,  years, months), nrow = 60,  ncol = 3)

rownames(m) = list(paste0("subj ", 1:60))   ### Why rownames() doesn't work? ###

2 个答案:

答案 0 :(得分:1)

因为您使用list,但rownames接受了一个向量。请改用:

rownames(m) = paste0("subj ", 1:60)

答案 1 :(得分:0)

我的偏好是让R的本质为你工作,因为它会迭代粘贴并分配名称。

row.names(m) <- paste0("subj " , 1:60)

它调用最少的函数并完成工作,