Konwersja ramki danych na wektor (według wierszy)
Mam ramkę danych z numerycznymi wpisami takimi jak ten
test <- data.frame(x = c(26, 21, 20), y = c(34, 29, 28))
Jak mogę uzyskać następujący wektor?
> 26, 34, 21, 29, 20, 28
Udało mi się uzyskać go za pomocą następującego, ale myślę, że powinien być znacznie bardziej elegancki sposób
X <- test[1, ]
for (i in 2:dim(test)[ 1 ]){
X <- cbind(X, test[i, ])
}
73
3 answers
Możesz spróbować as.vector(t(test))
. Należy pamiętać, że jeśli chcesz to zrobić za pomocą kolumn należy użyć unlist(test)
.
143
Author: teucer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-31 19:08:11
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-31 19:08:11
c(df$x, df$y)
# returns: 26 21 20 34 29 28
Jeśli konkretny porządek jest ważny to:
M = as.matrix(df)
c(m[1,], c[2,], c[3,])
# returns 26 34 21 29 20 28
Lub bardziej ogólnie:
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
9
Author: doug,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-30 13:42:19
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-30 13:42:19
Możesz spróbować tego, aby uzyskać kombinację:
as.numeric(rbind(test$x, test$y))
Który zwróci:
26, 34, 21, 29, 20, 28
0
Author: Wilinthon Bogoya Forero,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-07-31 03:12:16
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-07-31 03:12:16