英文:
How can I obtain a dataframe from a matrix (2D-array)?
问题
Trend Cluster
1 5
2 4
3 2
4 1
5 2
6 4
7 1
8 2
9 2
10 5
11 1
英文:
I'd like to transform my array into a dataframe, with the column "Cluster" where there will be the numbers 5,4,2,1, ... and "Trend" with numbers 1,2,3,4,...
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
cluster 5 4 2 1 2 4 1 2 2 5 1
What I expect :
Trend Cluster
1 5
2 4
3 2
4 1
...
答案1
得分: 1
With data.frame
:
data.frame(Trend = seq_along(mat), t(mat))
# Trend cluster
# 1 1 5
# 2 2 4
# 3 3 2
Reproducible data:
mat <- t(c(5, 4, 2))
rownames(mat) <- "cluster"
英文:
With data.frame
:
data.frame(Trend = seq_along(mat), t(mat))
# Trend cluster
# 1 1 5
# 2 2 4
# 3 3 2
Reproducible data:
mat <- t(c(5, 4, 2))
rownames(mat) <- "cluster"
答案2
得分: 0
cluster <- c(5,4,2,1,2,4,1,2,2,5,1);
data.frame('trend' = seq_len(length(cluster)), cluster)
An example: data.frame example
英文:
cluster <- c(5,4,2,1,2,4,1,2,2,5,1);
data.frame('trend' = seq_len(length(cluster)), cluster)
An example: data.frame example
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论