How to read and name different CSV files in R

前端 未结 2 1382
青春惊慌失措
青春惊慌失措 2021-01-27 03:02

I would like to work on several csv files to make some comparisons, so I wrote this code to read the different csv files I have:

path <- \"C:\\\\data\\\\\"
fi         


        
2条回答
  •  星月不相逢
    2021-01-27 03:48

    You're having a problem because your csv files have a blank column at the end... which makes your data end in a comma:

    04/09/14 00:01:00,04/09/14 00:01:00,2.221220E-003,5.797364E-004,0.000000E+000,1.641484E-003,

    This leads R to think your data consists of 7 columns rather than 6. The correct solution is to resave all your csv files correctly. Otherwise, R will see 7 columns but only 6 columnnames, and will logically think that the first column is rownames. Here you can apply the patch we came up with @konradrudolph:

    library(tibble) df %>% rownames_to_column() %>% setNames(c(colnames(.)[-1], 'DROP')) %>% select(-DROP)

    where df is the data from the csv. But patches like this can lead to unexpected results... better save the csv files correctly.

提交回复
热议问题