I want to set a column as an index in R.
x<-data.frame(x=c(1,4,5,6,7),y=c(5,7,8,5,9))
x
x y
1 1 5
2 4 7
3 5 8
4 6 5
5 7 9
I want to set x as an index and get the following output:
y
1 5
4 7
5 8
6 5
7 9
I want to set a column as an index in R.
x<-data.frame(x=c(1,4,5,6,7),y=c(5,7,8,5,9))
x
x y
1 1 5
2 4 7
3 5 8
4 6 5
5 7 9
I want to set x as an index and get the following output:
y
1 5
4 7
5 8
6 5
7 9
We can use column_to_rownames
from tibble
library(tibble)
x1 <- x %>%
column_to_rownames('x')
Or with deframe
deframe(x) %>%
data.frame(y = .)
Or with base R
`row.names<-`(x[-1], x$x)
# y
#1 5
#4 7
#5 8
#6 5
#7 9