User350178
User350178

Reputation: 59

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 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)

Upvotes: 1

Views: 44

Answers (2)

akrun
akrun

Reputation: 887128

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')

Upvotes: 2

shosaco
shosaco

Reputation: 6165

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"

Upvotes: 1

Related Questions