Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

R importing .csv data with date as first field erroneously

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...