42

Using R, how do I make a column of a dataframe the dataframe's index? Lets assume I read in my data from a .csv file. One of the columns is called 'Date' and I want to make that column the index of my dataframe.

For example in Python, NumPy, Pandas; I would do the following:

df = pd.read_csv('/mydata.csv')
d = df.set_index('Date')

Now how do I do that in R?

I tried in R:

df <- read.csv("/mydata.csv")
d <- data.frame(V1=df['Date'])
# or
d <- data.frame(Index=df['Date'])

# but these just make a new dataframe with one 'Date' column. 
#The Index is still 0,1,2,3... and not my Dates.
2

5 Answers 5

65

I assume that by "Index" you mean row names. You can assign to the row names vector:

rownames(df) <- df$Date
2
  • 2
    But it keeps Date as a column of data frame ... How to remove it from columns ?
    – scls
    Mar 10, 2016 at 20:24
  • 2
    You can remove it by assigning NULL to the columns: df$Date <- NULL Mar 10, 2016 at 20:54
23

The index can be set while reading the data, in both pandas and R.

In pandas:

import pandas as pd
df = pd.read_csv('/mydata.csv', index_col="Date")

In R:

df <- read.csv("/mydata.csv", header=TRUE, row.names="Date")
1
  • For R, you can also give the column number for row.names. For instance if you want the first column to be the index, you can give row.names=1.
    – nobot
    Jul 18 at 18:05
15

The tidyverse solution:

library(tidyverse)
df %>% column_to_rownames(., var = "Date")
0

The function match is very helpful when you need the indices of a first vector in a second vector; example: after tabulating one vector, I have obtained a table with 2 columns, the first one with the items and the second one with the frequency; suppose that you need to add a 3rd column to the frequency table, with the description of the data in the first column, that belongs to another dataset that has a complete list of your data in column1 of the frequency table, and the related name in another column (like a "dictionary"). First you save the match between the items in the first column of the frequency table with the items name column of the "dictionary" dataset; then you can use the saved match to access the related names.

-1

while saving the dataframe use row.names=F e.g. write.csv(prediction.df, "my_file.csv", row.names=F)

1
  • This answer is nothing to do with setting an index Mar 24, 2021 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.