我正在将 .csv 读入 R 中,其中有几种不同的变量类型,其中两种作为字符读入,尽管它们是数字的(以十进制度表示的纬度和经度)。为了解决这个问题,我在阅读它们后将它们定义为“as.numeric”。有没有更优雅的方法来做到这一点?也许在对“read.csv”的调用中?
d <- read.csv("data.csv",stringsAsFactors=F)
> str(d)
'data.frame': 467674 obs. of 7 variables:
$ station : chr "USC00036506" "USC00036506" "USC00036506" "USC00036506" ...
$ station_name: chr "SEARCY AR US" "SEARCY AR US" "SEARCY AR US" "SEARCY AR US" ...
$ lat : chr "35.25" "35.25" "35.25" "35.25" ...
$ lon : chr "-91.75" "-91.75" "-91.75" "-91.75" ...
$ tmax : int 50 50 39 100 72 61 -17 -44 6 0 ...
$ tmin : int -39 -39 -89 -61 -6 -83 -144 -150 -161 -128 ...
$ tobs : int 33 22 17 61 61 -78 -50 -94 -22 -11 ...
d$lat <- as.numeric(d$lat)
d$lon <- as.numeric(d$lon)
> str(d)
'data.frame': 467674 obs. of 7 variables:
$ station : chr "USC00036506" "USC00036506" "USC00036506" "USC00036506" ...
$ station_name: chr "SEARCY AR US" "SEARCY AR US" "SEARCY AR US" "SEARCY AR US" ...
$ lat : num 35.2 35.2 35.2 35.2 35.2 ...
$ lon : num -91.8 -91.8 -91.8 -91.8 -91.8 ...
$ tmax : int 50 50 39 100 72 61 -17 -44 6 0 ...
$ tmin : int -39 -39 -89 -61 -6 -83 -144 -150 -161 -128 ...
$ tobs : int 33 22 17 61 61 -78 -50 -94 -22 -11 ...