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.
brno792
  • 6,479
  • 17
  • 54
  • 71

5 Answers5

65

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

rownames(df) <- df$Date
Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
22

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")
bli
  • 7,549
  • 7
  • 48
  • 94
  • 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 '23 at 18:05
13

The tidyverse solution:

library(tidyverse)
df %>% column_to_rownames(., var = "Date")
Koot6133
  • 1,428
  • 15
  • 26
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.

C Lederman
  • 19
  • 4
-1

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

ah bon
  • 9,293
  • 12
  • 65
  • 148