Set a Data Frame Column as the Index of R data.frame object

后端 未结 3 2050
陌清茗
陌清茗 2020-12-29 20:17

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 ma

相关标签:
3条回答
  • 2020-12-29 20:41

    The tidyverse solution:

    library(tidyverse)
    df %>% column_to_rownames(., var = "Date")
    
    0 讨论(0)
  • 2020-12-29 20:45

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

    rownames(df) <- df$Date
    
    0 讨论(0)
  • 2020-12-29 21:02

    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")
    
    0 讨论(0)
提交回复
热议问题