评价错误:无法强制类型'关闭'到类型'字符的向量

时间:2017-08-21 21:34:14

标签: r

运行以下代码时出现错误

weather <- as.tibble(fread("../input/weather-data-in-new-york-city-2016/weather_data_nyc_centralpark_2016.csv"))

> weather <- weather %>%>
+   mutate(date = dmy(date),
+          rain = as.numeric(ifelse(precipitation == "T", "0.01", precipitation)),
+          s_fall = as.numeric(ifelse(`snow fall` == "T", "0.01", `snow fall`)),
+          s_depth = as.numeric(ifelse(`snow depth` == "T", "0.01", `snow depth`)),
+          all_precip = s_fall + rain,
+          has_snow = (s_fall > 0) | (s_depth > 0),
+          has_rain = rain > 0,
+          max_temp = `maximum temerature`,
+          min_temp = `minimum temperature`)

Error in mutate_impl(.data, dots) : 
  Evaluation error: cannot coerce type 'closure' to vector of type 'character'.

以下是数据表的详细信息。

> str(weather)

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   366 obs. of  1 variable:
 $ date,maximum temerature,minimum temperature,average temperature,precipitation,snow fall,snow depth: chr  "1-1-2016,42,34,38.0,0.00,0.0,0" "2-1-2016,40,32,36.0,0.00,0.0,0" "3-1-2016,45,35,40.0,0.00,0.0,0" "4-1-2016,36,14,25.0,0.00,0.0,0" ...
 - attr(*, ".internal.selfref")=<externalptr> 

1 个答案:

答案 0 :(得分:0)

根据您在str(weather)中发布的详细信息,它目前将每个列合并为一个字符类列,名为date,maximum temerature,minimum temperature,average temperature,precipitation,snow fall,snow depth

This post讨论了分割数据帧的各种方法。我使用hadley的separate包中的tidyr来操作您的数据集,但您也可以尝试其余的数据集:

# reproducible dataset based on OP's data frame details
weather <- data.frame(`date,maximum temerature,minimum temperature,average temperature,precipitation,snow fall,snow depth` = 
                           c("1-1-2016,42,34,38.0,0.00,0.0,0",
                             "2-1-2016,40,32,36.0,0.00,0.0,0",
                             "3-1-2016,45,35,40.0,0.00,0.0,0",
                             "4-1-2016,36,14,25.0,0.00,0.0,0"),
                      stringsAsFactors = F, check.names = FALSE)

library(dplyr); library(tidyr)

new.column.names <- colnames(weather)[1] %>% strsplit(",") %>% unlist()
colnames(weather)[1] <- "single.column" # rename the column, because that column name is just asking for typos

weather <- weather %>%
  separate(col = single.column, 
           into = new.column.names, 
           sep = ",")