英文:
Counting the number of elements in a list and then taking the counting as a list in R
问题
I have a vector:
x <- c(0.8,1.0,661.7,661.8,661.9,662.3,662.6,662.7,663.3,663.6,663.7)
I have used function as.data.frame(x)
to make the following data frame:
X1
1 0.8
2 1.0
3 661.7
4 661.8
5 661.9
6 662.3
7 662.6
8 662.7
9 663.3
10 663.6
11 663.7
How can I take the list of labels for each element?
i.e x0 <- c(1,2,3,4,5,6,7,8,9,10,11)
英文:
I have a vector:
x <- c(0.8,1.0,661.7,661.8,661.9,662.3,662.6,662.7,663.3,663.6,663.7)
I have used function as.data.frame(x)
to make following data frame:
X1
1 0.8
2 1.0
3 661.7
4 661.8
5 661.9
6 662.3
7 662.6
8 662.7
9 663.3
10 663.6
11 663.7
How can I take the list of labels for each element?
i.e x0 <- c(1,2,3,4,5,6,7,8,9,10,11)
答案1
得分: 2
我们可以有一个名为vector
的变量,并使用stack
,这也可以灵活地具有不同的标签。
或者使用rownames_to_column
:
library(dplyr)
as.data.frame(x) %>%
rownames_to_column('rn')
英文:
We can have a named vector
and use stack
and this is also flexible in having different labels
stack(setNames(x, seq_along(x)))
Or using rownames_to_column
library(dplyr)
as.data.frame(x) %>%
rownames_to_column('rn')
答案2
得分: 1
If you already created that dataframe, take out the rownames:
x <- c(0.8,1.0,661.7,661.8,661.9,662.3,662.6,662.7,663.3,663.6,663.7)
rownames(as.data.frame(x))
#> [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11"
英文:
If you already created that dataframe, take out the rownames:
x <- c(0.8,1.0,661.7,661.8,661.9,662.3,662.6,662.7,663.3,663.6,663.7)
rownames(as.data.frame(x))
#> [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论