这一直困扰着我.考虑以下:
# Part A #
# Make a silly simple matrix with column names
x = matrix(1:4, ncol = 2)
colnames(x) = c("a","b")
# Part B #
# Pick out the first row of the matrix. This is not a matrix,
# and the column names are no longer accessible by colnames()
y = x[1,]
y
is.matrix(y)
colnames(y)
# Part C #
# But here is what I want:
y = matrix(x[1,], nrow = 1, dimnames = list(c(), colnames(x)))
Run Code Online (Sandbox Code Playgroud)
有没有办法用更少的处理步骤或更少的代码来实现C部分?似乎应该有一个命令几乎x[1,]与同样的事情一样短.