Reputation: 923
I have a simple problem but I just don't know how to fix it. I have a dataframe like this:
structure(list(ID = c(1006332, 1010660,
1012960, 1013515, 1014490), ave_ocean = c(1, 0, 0, 0, 0), ave_price_per_sqft = c(1419.69,
912.18, 600.74, 673.8725, 439.46), ave_year = c(2005, 2009, 1986.4,
2006.25, 1983), ave_DOM = c(7, 10, 36.1, 10.5, 104), total_num_sold = c(1L,
1L, 10L, 4L, 1L)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
ID ave_ocean ave_price_per_sqft ave_year ave_DOM total_num_sold
<dbl> <dbl> <dbl> <dbl> <dbl> <int>
1 1006332 1 1420. 2005 7 1
2 1010660 0 912. 2009 10 1
3 1012960 0 601. 1986. 36.1 10
4 1013515 0 674. 2006. 10.5 4
5 1014490 0 439. 1983 104 1
I want to set column ID as row name and then remove it. I use the code below:
row.names(data) <- data$ID
data <-data[,-c(1)]
when I run this it first changes the row name and replaces it with ID but once I run the second line to remove "ID" column it changes row name to the normal row name! Any idea? Any alternative solution?
Upvotes: 0
Views: 355
Reputation: 5232
tibble::column_to_rownames(df, "ID")
read documentation (first sentance) ?tibble::column_to_rownames
, also note that return value is data.frame
beacuse of that reason.
Upvotes: 2
Reputation: 388982
The first line sets the rownames but you'll not be able to see it since you have a tibble.
row.names(data) <- data$ID
#$Warning message:
#Setting row names on a tibble is deprecated.
data
# A tibble: 5 x 6
# ID ave_ocean ave_price_per_sqft ave_year ave_DOM total_num_sold
#* <dbl> <dbl> <dbl> <dbl> <dbl> <int>
#1 1006332 1 1420. 2005 7 1
#2 1010660 0 912. 2009 10 1
#3 1012960 0 601. 1986. 36.1 10
#4 1013515 0 674. 2006. 10.5 4
#5 1014490 0 439. 1983 104 1
If you change your data to dataframe you'll see the rownames.
data <- data.frame(data)
df
# ID ave_ocean ave_price_per_sqft ave_year ave_DOM total_num_sold
#1006332 1006332 1 1419.6900 2005.00 7.0 1
#1010660 1010660 0 912.1800 2009.00 10.0 1
#1012960 1012960 0 600.7400 1986.40 36.1 10
#1013515 1013515 0 673.8725 2006.25 10.5 4
#1014490 1014490 0 439.4600 1983.00 104.0 1
Upvotes: 0