In R, we can change the row names of a data frame using the rownames() function. The rownames() function takes two arguments: the data frame and a vector of new row names. Here is an example R program that demonstrates how to change row names of a data frame:
# create a sample data frame df <- data.frame(x = 1:3, y = 4:6) # display the current row names rownames(df) # create a vector of new row names new_row_names <- c("A", "B", "C") # set the new row names using the rownames() function rownames(df) <- new_row_names # display the updated row names rownames(df)
In this example, we first create a data frame df
with two columns x
and y
. We then display the current row names using the rownames()
function. Next, we create a vector new_row_names
of new row names. Finally, we set the new row names using the rownames()
function and display the updated row names.
Note that the vector of new row names must have the same length as the number of rows in the data frame. If the vector of new row names has a different length, R will return an error.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.