Closed hehuanshu96 closed 4 years ago
@hehuanshu96
# remotes::install_github("GuangchuangYu/nCov2019")
library(nCov2019)
# setwd("F:/rwork/covid2019")
# 用相对路径
library(nCov2019)
x <- load_nCov2019()
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.1
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
for (i in unique(x[["data"]][["province"]])) {
dat.temp <- x[["data"]][i, c(1:6, 9:11)] %>%
write.csv(file = file.path("liyongtao/data", paste(i, '.csv', sep = '')))
}
class(x[["data"]])
## [1] "data.frame"
class(x)
## [1] "nCov2019History"
x[["data"]]
才是一个 `data.framewrite.csv(file = paste(i, '.csv', sep = ''))
这个地方是存储当前路径,我这里进行修改setwd("F:/rwork") library(nCov2019) library(dplyr) x <- load_nCov2019()
for (i in unique(x[["data"]][["province"]])) {
- dat.temp <- x[["data"]][i, c(1:6, 9:11)] %>%
- write.csv(file = file.path("covid2019/data", paste(i, '.csv', sep = '')))
- } Error in
[.data.frame
(x[["data"]], i, c(1:6, 9:11)) : undefined columns selected
@hehuanshu96
# setwd("F:/rwork") #不要使用这行代码,没必要。
library(nCov2019)
library(tidyverse)
## -- Attaching packages -------------------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.2.1 √ purrr 0.3.3
## √ tibble 2.1.3 √ dplyr 0.8.3
## √ tidyr 0.8.3 √ stringr 1.4.0
## √ readr 1.3.1 √ forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.6.2
## Warning: package 'purrr' was built under R version 3.6.1
## Warning: package 'dplyr' was built under R version 3.6.1
## -- Conflicts ----------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
x <- load_nCov2019()
data_path <- "data"
dir.create(data_path, recursive = TRUE)
## Warning in dir.create(data_path, recursive = TRUE): 'data'已存在
for (i in unique(x[["data"]][["province"]])) {
x[["data"]][i, c(1:6, 9:11)] %>%
write_excel_csv(file.path(data_path, paste(i, '.csv', sep = '')))
}
fs::dir_ls(data_path)
## data/上海.csv data/云南.csv data/内蒙古.csv data/北京.csv data/台湾.csv data/吉林.csv
## data/四川.csv data/天津.csv data/宁夏.csv data/安徽.csv data/山东.csv data/山西.csv
## data/广东.csv data/广西.csv data/新疆.csv data/江苏.csv data/江西.csv data/河北.csv
## data/河南.csv data/浙江.csv data/海南.csv data/湖北.csv data/湖南.csv data/澳门.csv
## data/甘肃.csv data/福建.csv data/西藏.csv data/贵州.csv data/辽宁.csv data/重庆.csv
## data/陕西.csv data/青海.csv data/香港.csv data/黑龙江.csv
dat.temp <-
可以去掉,因为 data %>% write.csv
是把数据导出,不需要把这个操作赋值到变量dat.temp
上。
这部分数据导入和导出的问题,参考下
https://github.com/JiaxiangBU/tutoring2/issues/25#issuecomment-590035565
git clone https://github.com/JiaxiangBU/tutoring2.git
clone 这个项目后,用 RStudio 打开它。
打开这个 Rmd ,在项目路径的liyongtao/write_csv.Rmd
,你就可以直接复现这个例子了。
[Workspace loaded from F:/rwork/.RData]
library(nCov2019) library(tidyverse) -- Attaching packages --------------------------------------- tidyverse 1.3.0 -- √ ggplot2 3.2.1 √ purrr 0.3.3 √ tibble 2.1.3 √ dplyr 0.8.4 √ tidyr 1.0.2 √ stringr 1.4.0 √ readr 1.3.1 √ forcats 0.4.0 -- Conflicts ------------------------------------------ tidyverse_conflicts() -- x dplyr::filter() masks stats::filter() x dplyr::lag() masks stats::lag() x <- load_nCov2019() data_path <- "data" dir.create(data_path,recursive = TRUE) Warning message: In dir.create(data_path, recursive = TRUE) : 'data'已存在
for (i in unique(x[["data"]][["province"]])){
- x[["data"]][i, c(1:6,9:11)] %>%
- write_excel_csv(file.path(),paste(i,'.csv',sep = ""))
- } Error in
[.data.frame
(x[["data"]], i, c(1:6, 9:11)) : undefined columns selected sessionInfo() R version 3.6.2 (2019-12-12) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936
[2] LC_CTYPE=Chinese (Simplified)_People's Republic of China.936
[3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_People's Republic of China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] forcats_0.4.0 stringr_1.4.0 dplyr_0.8.4 purrr_0.3.3
[5] readr_1.3.1 tidyr_1.0.2 tibble_2.1.3 ggplot2_3.2.1
[9] tidyverse_1.3.0 nCov2019_0.0.8
loaded via a namespace (and not attached):
[1] Rcpp_1.0.3 cellranger_1.1.0 pillar_1.4.3
[4] compiler_3.6.2 dbplyr_1.4.2 remotes_2.1.1
[7] tools_3.6.2 digest_0.6.25 lubridate_1.7.4
[10] jsonlite_1.6.1 lifecycle_0.1.0 gtable_0.3.0
[13] nlme_3.1-144 lattice_0.20-38 pkgconfig_2.0.3
[16] rlang_0.4.4 reprex_0.3.0 cli_2.0.1
[19] DBI_1.1.0 rstudioapi_0.11 haven_2.2.0
[22] withr_2.1.2 xml2_1.2.2 httr_1.4.1
[25] downloader_0.4 fs_1.3.1 hms_0.5.3
[28] generics_0.0.2 vctrs_0.2.3 grid_3.6.2
[31] tidyselect_1.0.0 glue_1.3.1 R6_2.4.1
[34] fansi_0.4.1 readxl_1.3.1 modelr_0.1.6
[37] magrittr_1.5 scales_1.1.0 backports_1.1.5
[40] rvest_0.3.5 assertthat_0.2.1 colorspace_1.4-1
[43] stringi_1.4.6 lazyeval_0.2.2 munsell_0.5.0
[46] broom_0.5.4 crayon_1.3.4
前面的sessionInfo()
我进行了删除,为了你每次打开这个issue简洁和快一些。
# x <- load_nCov2019()
# x %>% write_rds("x.rds")
# 为了方便你调用,我把 x 这个对象离线保存下来了
x <- readr::read_rds("x.rds")
data_path <- "data"
dir.create(data_path, recursive = TRUE)
## Warning in dir.create(data_path, recursive = TRUE): 'data'已存在
for (i in unique(x[["data"]][["province"]])) {
x[["data"]][i, c(1:6, 9:11)] %>%
write_excel_csv(file.path(data_path, paste(i, '.csv', sep = '')))
}
fs::dir_ls(data_path)
## data/上海.csv data/云南.csv data/内蒙古.csv data/北京.csv data/台湾.csv data/吉林.csv
## data/四川.csv data/天津.csv data/宁夏.csv data/安徽.csv data/山东.csv data/山西.csv
## data/广东.csv data/广西.csv data/新疆.csv data/江苏.csv data/江西.csv data/河北.csv
## data/河南.csv data/浙江.csv data/海南.csv data/湖北.csv data/湖南.csv data/澳门.csv
## data/甘肃.csv data/福建.csv data/西藏.csv data/贵州.csv data/辽宁.csv data/重庆.csv
## data/陕西.csv data/青海.csv data/香港.csv data/黑龙江.csv
我搭建了一个临时环境,如图
你可以在这个临时环境测试代码。 临时环境启动的时候,稍等2-3分钟就构建完成。
http://beta.mybinder.org/v2/gh/JiaxiangBU/tutoring2/master?urlpath=rstudio
打开文件liyongtao/write_csv.Rmd
@hehuanshu96
> remotes::install_git('https://gitee.com/GuangchuangYu/nCov2019')
> library(nCov2019)
> library(tidyverse)
-- Attaching packages --------------------------------------- tidyverse 1.3.0 --
√ ggplot2 3.2.1 √ purrr 0.3.3
√ tibble 2.1.3 √ dplyr 0.8.4
√ tidyr 1.0.2 √ stringr 1.4.0
√ readr 1.3.1 √ forcats 0.4.0
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
> data_path <- "data"
> dir.create(data_path, recursive = TRUE)
Warning message:
In dir.create(data_path, recursive = TRUE) : 'data'已存在
> for (i in unique(x[["data"]][["province"]])) {
+ x[["data"]][i, c(1:6, 9:11)] %>%
+ write_excel_csv(file.path(data_path, paste(i, '.csv', sep = '')))
+ }
这个运行没有问题。就是保存的excel是空表格
git clone https://github.com/JiaxiangBU/tutoring2.git
clone 这个项目后,用 RStudio 打开它。 打开这个 Rmd ,在项目路径的liyongtao/write_csv.Rmd,你就可以直接复现这个例子了。
这里参考 notes https://jiaxiangbu.github.io/learn_git/output/git-github-gitlab-learning-notes.html 2 clone 第一个项目
@hehuanshu96
空值的原因找到了以为这行语法写错了。
df[df$province==i, c(1:6, 9:11)]
才是正确的, df[i,c(1:6, 9:11)]]
中i必须是连续
int,不能是文本。
i in unique(df[["province"]])
我们知道省份这个字段都是文本,所以导致 df[i,c(1:6, 9:11)]]
筛选出来都是空。
# setwd("F:/rwork") #不要使用这行代码,没必要。
# library(nCov2019)
library(tidyverse)
## -- Attaching packages -------------------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.2.1 √ purrr 0.3.3
## √ tibble 2.1.3 √ dplyr 0.8.3
## √ tidyr 0.8.3 √ stringr 1.4.0
## √ readr 1.3.1 √ forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.6.2
## Warning: package 'purrr' was built under R version 3.6.1
## Warning: package 'dplyr' was built under R version 3.6.1
## -- Conflicts ----------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
# x <- load_nCov2019()
# x %>% write_rds("x.rds")
# 为了方便你调用,我把 x 这个对象离线保存下来了
x <- readr::read_rds("x.rds")
df <- x[['data']]
data_path <- "data"
dir.create(data_path, recursive = TRUE)
## Warning in dir.create(data_path, recursive = TRUE): 'data'已存在
for (i in unique(df[["province"]])) {
df[df$province==i, c(1:6, 9:11)] %>%
write_excel_csv(file.path(data_path, paste(i, '.csv', sep = '')))
}
dir_ls0 <- fs::dir_ls(data_path)
dir_ls0
## data/上海.csv data/云南.csv data/内蒙古.csv data/北京.csv data/台湾.csv data/吉林.csv
## data/四川.csv data/天津.csv data/宁夏.csv data/安徽.csv data/山东.csv data/山西.csv
## data/广东.csv data/广西.csv data/新疆.csv data/江苏.csv data/江西.csv data/河北.csv
## data/河南.csv data/浙江.csv data/海南.csv data/湖北.csv data/湖南.csv data/澳门.csv
## data/甘肃.csv data/福建.csv data/西藏.csv data/贵州.csv data/辽宁.csv data/重庆.csv
## data/陕西.csv data/青海.csv data/香港.csv data/黑龙江.csv
read_csv(dir_ls0[1]) %>% head()
## Parsed with column specification:
## cols(
## province = col_character(),
## city = col_character(),
## time = col_date(format = ""),
## cum_confirm = col_double(),
## cum_heal = col_double(),
## cum_dead = col_double(),
## confirm = col_character(),
## dead = col_double(),
## heal = col_double()
## )
## # A tibble: 6 x 9
## province city time cum_confirm cum_heal cum_dead confirm dead heal
## <chr> <chr> <date> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
## 1 上海 上海 2020-02-13 318 62 1 <NA> NA NA
## 2 上海 上海 2020-02-12 313 57 1 <NA> NA NA
## 3 上海 上海 2020-02-11 306 53 1 <NA> NA NA
## 4 上海 上海 2020-02-10 302 48 1 <NA> NA NA
## 5 上海 上海 2020-02-09 295 44 1 <NA> NA NA
## 6 上海 上海 2020-02-08 292 41 1 <NA> NA NA
@hehuanshu96
简单描述你的问题和希望产生的输出结果。 事先可以在以下项目查看是否已经有解决方案
remotes::install_github("GuangchuangYu/nCov2019")
library(nCov2019) setwd("F:/rwork/covid2019") library(nCov2019) x <- load_nCov2019() library(dplyr) for (i in unique(x[["data"]][["province"]])) { dat.temp <- x[i, c(1:6, 9:11)] %>% write.csv(file = paste(i,'.csv',sep = '')) }
问题:Error in
[.data.frame
(obj, ii, j, drop = FALSE) : undefined columns selected相关问题,assign 给对应 owner,打上对应的标签 label 等。
问题解决后,记得 close。
简述问题
...
希望输出的结果
...
过程中产生的报错 (可选)
...
遇到报错的时候,推荐采用 debugging 思路,定位具体报错信息和报错的函数或者对象,具体可以参考例子 https://jiaxiangbu.github.io/learn_rstudioapi/analysis/rstudio-and-api-learning-notes.html#section-6.2
在书写 Rmd 文档时,
library
放在前面,先让第三方包 import,这样可以让后面的函数顺利执行。here
函数,?here::here
查看使用方法source
调用。GBK
的,修改为UTF-8
,方法是在 RStudio 点击左上角的 File,点击 Save with Encoding,选择UTF-8
有关联的问题
直接把链接复制在此。
复现代码和数据
首次提问,可以建立自己全拼文件夹,见 https://github.com/JiaxiangBU/tutoring 在当前文件夹下,
把反馈的截图代码和输入数据上传,我在你的代码上进行修改。
output
文件夹.Rmd
,.md
) 文档传analysis
文件夹.md
可以展示目前代码状况,不需要我执行代码就可以看到结果,方便快速查看问题。190101-who_am_i-name.postfix
我们接下来反馈的时候给上有复现的代码和数据,这样我可以很快给你 debug。
代码尽量避免截图,因为无法复制截图复现,因此更好的方式复制代码。
如果是 R 代码,按照以下输入
如果是 SQL 代码,按照以下输入
Todos