我有一个像这样的数字条目的数据框
test <- data.frame(x = c(26, 21, 20), y = c(34, 29, 28))
如何得到下面的矢量?
> 26, 34, 21, 29, 20, 28
我可以使用下面的方法得到它,但是我想应该有一个更优雅的方法
X <- test[1, ] for (i in 2:dim(test)[ 1 ]){ X <- cbind(X, test[i, ]) }
c(df$x, df$y) # returns: 26 21 20 34 29 28
if the particular order is important then:
M = as.matrix(df) c(m[1,], c[2,], c[3,]) # returns 26 34 21 29 20 28
Or more generally:
m = as.matrix(df) q = c() for (i in seq(1:nrow(m))){ q = c(q, m[i,]) } # returns 26 34 21 29 20 28
You can try as.vector(t(test)). Please note that, if you want to do it by columns you should use unlist(test).
as.vector(t(test))
unlist(test)
You can try this to get your combination:
as.numeric(rbind(test$x, test$y))
which will return:
26, 34, 21, 29, 20, 28