我有以下代码,应该将pm25,fips,经度和纬度显示为数字,这样我可以做五个数字摘要,而是将其显示为一个字符。我不明白为什么。我可以将每列转换为数字,但在处理大量数据表时效率太低。
同样问题是为什么pm25,fips,经度和纬度显示为字符?
x <- data.frame(9, 01003, "east", -87.75, 30.59)
colnames(x) <- c("pm25", "fips", "region", "longitude", "latitude")
x
x <- rbind(x, c(9, 01027, "east", -85.84, 33.27))
x <- rbind(x, c(10, 01033, "east", -87.73, 34.73))
x <- rbind(x, c(11, 01049, "east", -85.80, 34.46))
x <- rbind(x, c(12, 01055, "east", -86.03, 34.02))
x <- rbind(x, c(10, 01069, "east", -85.35, 31.19))
x
#Five-number summary
summary(x$pm25)
summary(x$fips)
summary(x$longitude)
summary(x$latitude)
结果:
> x <- data.frame(9, 01003, "east", -87.75, 30.59)
> colnames(x) <- c("pm25", "fips", "region", "longitude", "latitude")
> x
pm25 fips region longitude latitude
1 9 1003 east -87.75 30.59
> x <- rbind(x, c(9, 01027, "east", -85.84, 33.27))
> x <- rbind(x, c(10, 01033, "east", -87.73, 34.73))
> x <- rbind(x, c(11, 01049, "east", -85.80, 34.46))
> x <- rbind(x, c(12, 01055, "east", -86.03, 34.02))
> x <- rbind(x, c(10, 01069, "east", -85.35, 31.19))
> x
pm25 fips region longitude latitude
1 9 1003 east -87.75 30.59
2 9 1027 east -85.84 33.27
3 10 1033 east -87.73 34.73
4 11 1049 east -85.8 34.46
5 12 1055 east -86.03 34.02
6 10 1069 east -85.35 31.19
>
> #Five-number summary
> summary(x$pm25)
Length Class Mode
6 character character
> summary(x$fips)
Length Class Mode
6 character character
> summary(x$longitude)
Length Class Mode
6 character character
> summary(x$latitude)
Length Class Mode
6 character character
答案 0 :(得分:1)
这是Rich Scriven对我的问题的评论的答案。 由于他没有在这里回答,并且为了结束这个问题,我将赞美自己。如果他在这里回答,我会给予他信任。 如果可以的话,给他信用/投票。
c(9, 01027, "east", -85.84, 33.27)
生成一个字符向量。您需要list()
而不是c()
。
答案 1 :(得分:1)
您需要将摘要称为
summary(as.numeric(x$fips))
所以你的代码就像
x <- data.frame(9, 01003, "east", -87.75, 30.59)
colnames(x) <- c("pm25", "fips", "region", "longitude", "latitude")
x <- rbind(x, c(9, 01027, "east", -85.84, 33.27))
x <- rbind(x, c(10, 01033, "east", -87.73, 34.73))
x <- rbind(x, c(11, 01049, "east", -85.80, 34.46))
x <- rbind(x, c(12, 01055, "east", -86.03, 34.02))
x <- rbind(x, c(10, 01069, "east", -85.35, 31.19))\
summary(as.numeric(x$fips))
# summary(as.numeric(x$fips))
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 1003 1028 1041 1039 1054 1069