代码之家  ›  专栏  ›  技术社区  ›  Nelson

使用子集识别和纠正数据集中的打字错误

  •  0
  • Nelson  · 技术社区  · 7 年前

    我有一个数据集:(可以从下面的链接中看到) https://drive.google.com/file/d/0B4Mldbnr1-avMDIxYmZLSnRfUDA/view?usp=sharing 我想用子集&级别功能。以下是我一直在尝试应用的内容,但似乎不起作用:

    # Setting working directory
    setwd("F:/Intro Data Science/Assignment Part B/Assignment Part B-20170902")
    plot.new()
    options(digits=2)
    
    # 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"
    

    我真的需要你们的帮助。这本应该很容易,因为我完全遵循了讲座中给出的内容,但当我运行代码时,它没有做任何事情。 谢谢你的帮助 纳尔逊

    请求的输出:

    > dput(head(power, 30))
    structure(list(Area = c(144.38, 176.83, 268.71, 208.67, 123.61, 
    199.3, 109.46, 183.28, 110.61, 146.91, 77.451, 232.65, 270.94, 
    49.191, 234.5, 280.93, 192.18, 95.918, 230.74, 72.698, 129.26, 
    110.76, 199.44, 129.75, 146.8, 287.97, 162.1, 249.03, 159.3, 
    272.51), City = c("Auckland ", "Auckland ", "Auckland ", "Auckland ", 
    "Auckland ", "Auckland ", "Auckland ", "Auckland ", "Auckland ", 
    "Auckland ", "Auckland ", "Auckland ", "Auckland ", "Auckland ", 
    "Auckland ", "Auckland ", "Auckland ", "Ackland ", "Auckland ", 
    "Auckland ", "Auckland ", "Auckland ", "Auckland ", "Auckland ", 
    "Auckland ", "Auckland ", "Auckland ", "Auckland ", "Auckland ", 
    "Auckland "), P.Winter = c(1684.9, 1926.7, 2026.9, 1938.1, 1579.9, 
    1991.4, 1572.5, 1691.2, 1684.2, 1743.6, 1234.6, 2043, 1986.7, 
    1259.7, 1870.4, 2115.6, 18000, 1452, 1936.2, 1430.2, 1587.3, 
    1614.3, 1993.2, 1746.4, 1807.6, 2009.4, 1859.1, 1985.5, 1909.4, 
    1892.7), P.Summer = c(1194.5, 1487.3, 1737.3, -158, 1148.1, 1445.8, 
    885.77, 1393, 1191.5, 1149.9, 813.38, 1623.8, 1708, 874.48, 1635.7, 
    1826.1, 1596.6, 793.71, 1668.8, 905.6, 1227.3, 938.38, 1523.1, 
    1012.6, 1122.8, 1829.5, 1223.3, 1653.2, 1175.5, 1882)), .Names = c("Area", 
    "City", "P.Winter", "P.Summer"), row.names = c(NA, 30L), class = "data.frame")
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Rui Barradas    7 年前

    我相信你想要的功能是 droplevels .
    首先,编一些数据。

    set.seed(5295)    # make the results reproducible
    cities <- factor(sample(c("Ackland", "Auckland", "Wellington", "Sidney"), 100, TRUE))
    power <- data.frame(City = cities)
    

    现在是代码,从你的开始。

    power$City[power$City == "Ackland"] <- "Auckland"
    power$City <- droplevels(power$City)
    
    levels(power$City)    # check if it worked
    #[1] "Auckland"   "Sidney"     "Wellington"
    

    编辑。
    在看到 dput(head(power, 30)) ,解决方案变得显而易见。该列 City character factor ,并且没有值 "Ackland" "Auckland" ,它们后面有一个空白,这会把事情搞砸。所以我们需要做的就是删除 "Ackland " 并删除尾随空格。

    str(power)
    #'data.frame':   30 obs. of  4 variables:
    # $ Area    : num  144 177 269 209 124 ...
    # $ City    : chr  "Auckland " "Auckland " "Auckland " "Auckland " ...
    # $ P.Winter: num  1685 1927 2027 1938 1580 ...
    # $ P.Summer: num  1194 1487 1737 -158 1148 ...
    
    which(power$City == "Ackland ")    # note the white space
    #[1] 18
    
    which(power$City == "Auckland ")    # note the white space
    # [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 19 20 21 22 23 24 25 26
    #[26] 27 28 29 30
    
    # remove the value "Ackland ", with white space
    power$City[power$City == "Ackland "] <- "Auckland"
    power$City <- trimws(power$City)    # remove white spaces from all of them
    

    没有列消失,只需运行 str(power) 去看它。