数据清理和表中拼写错误的单词

时间:2017-10-15 21:14:26

标签: r subset data-cleaning

我有这个CSV数据集,我需要创建一个函数来执行数据清理,但仍然无法正常工作,而且我的想法已经用完了。

以下是Google云端硬盘上的dataset

以下是我需要做的事情:

  • 纠正可能的拼写错误
  • 删除不相关的数据(仅考虑奥克兰和惠灵顿的房屋)
  • 删除异常值,例如负面积,负功耗,非常高的面积,非常高的功耗

到目前为止,这是我所做的代码:

# Reading data set
installed.packages("lubridate")
library(lubridate)

# Reading data set
power <- read.csv("data set 6.csv", na.strings="")

# SUBSETTING
Area <- as.numeric(power$Area)
City <- as.character(power$City)
P.Winter <- as.numeric(power$P.Winter)
P.Summer <- as.numeric(power$P.Summer)

#Data Cleaning
levels(power$City) <- c(levels(power$City), "Auckland")
power$City[power$City == "Ackland"] <- "Auckland"

#Removing irrelevant data (only houses in Auckland and Wellington are considered)
power$City <- power$City[-c(496,499), ]

运行此代码后,拼写错误的单词(“Ackland”)不会像我预期的那样改为奥克兰。 此图中显示的突出显示的行应该更改为奥克兰: enter image description here

1 个答案:

答案 0 :(得分:2)

解决您的问题崩溃因素水平&#39; Ackland&#39;和奥克兰&#39; (并且假设你想要力量$ City是/仍然是一个因素):

一种方法是将levels()函数传递给一个命名列表,每个名称都是所需级别的正确标签(在您的情况下是数据集中城市的正确名称),请参阅: Cleaning up factor levels (collapsing multiple levels/labels)作为一般例子。

然而,就像抬头一样,请注意数据集中Ackland和Auckland字符串背后的额外空间:

    # first view classes to confirm power$City is a factor
     > apply(power, class)     # --> or is.factor(power$City) will work to
        Area      City  P.Winter  P.Summer 
    "numeric"  "factor" "numeric" "numeric" 

    # Notice spaces behind "Ackland " and "Auckland "
     > levels(power$City)
    [1] "Ackland "   "Auckland "  "Sydney"     "Wellington"

在考虑空格后,将命名列表传递给levels()即可运行:

    levels(power$City) <-  list(Auckland = c("Ackland ", "Auckland "), Sydney = c("Sydney"), Wellington = c("Wellington"))

    # Now only three factor levels (notice this also took care of the extra spaces)
      > levels(power$City)
     [1] "Auckland"   "Sydney"     "Wellington"

你现在有3个级别而不是4级,注意这也处理了级别标签中的空格

子集仅包含相关城市

       subpower <- power[which(power$City == c("Auckland", "Wellington")), ]

你也可以通过子集来排除负值,极值等......

注意:我唯一真正的贡献就是抓住额外的空间,自己解决类似的问题Aaron's 回答非常有帮助。希望这有帮助!