-1

This is absolutely maddening! I am trying to import a basic, simple file that has dates as the first field. If I import the file as-is, it renders the dates into row names. If, however, I go into the .csv file and manually change ANYthing at all, in any field, the import is fine. It's as if the CSV file has some sort of hidden nonsense somewhere.

Here is a link to the files- any of the CSV files within are doing the same thing. This is from the SWAT weather data network here. Any of the data I request from them does this.

What is happening here? I've imported literally thousands of CSV files in my life and have never come across this. This is a very basic, straightforward file.

Ryan Utz
  • 51
  • 1
  • 6
  • 1
    If looking for help, please make sure to include a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also, no one will be able to help you if you do not provide any code that you're using. There are a thousand different ways of reading a .csv into R. – Ben Dec 31 '20 at 16:54
  • Ben- if I code up some basic stuff, I can't reproduce it. The files I just linked to in my edited plea for help all do it. Just try importing it in the most basic read.csv or read.delim way and look at the dataframe. – Ryan Utz Dec 31 '20 at 20:30

1 Answers1

0

I can replicate the problem. It may have something to do with the trailing , at the end of each line so that the number of column names is less than the number of columns to be imported. That would trigger using the first column as row labels. It seems to work to skip the first line and provide your own column names with an extra one:

coln <- c("Date", "Longitude", "Latitude", "Elevation", "Max Temperature",
     "Min Temperature", "Precipitation", "Wind", "Relative Humidity", "Solar", "X")
weather <- read.csv("weatherdata-401-1053.csv", header=FALSE, skip=1, col.names=coln)
str(weather)
# 'data.frame': 4018 obs. of  11 variables:
#  $ Date             : chr  "1/1/2000" "1/2/2000" "1/3/2000" "1/4/2000" ...
#  $ Longitude        : num  -105 -105 -105 -105 -105 ...
#  $ Latitude         : num  40.1 40.1 40.1 40.1 40.1 ...
#  $ Elevation        : int  1890 1890 1890 1890 1890 1890 1890 1890 1890 1890 ...
#  $ Max.Temperature  : num  4.414 1.116 -5.891 2.193 0.503 ...
#  $ Min.Temperature  : num  -4.78 -6.51 -11.51 -10.77 -8.26 ...
#  $ Precipitation    : num  0.378 1.054 1.552 0 0.381 ...
#  $ Wind             : num  3.69 2.48 3.89 5.92 4.02 ...
#  $ Relative.Humidity: num  0.684 0.625 0.498 0.478 0.479 ...
#  $ Solar            : num  9.96 8.7 6.19 10.52 10.2 ...
#  $ X                : logi  NA NA NA NA NA NA ...

Then just delete the blank column at the end.

dcarlson
  • 10,936
  • 2
  • 15
  • 18