Upgrade to Pro — share decks privately, control downloads, hide ads and more …

前処理R 第2回資料

前処理R 第2回資料

第2回 前処理R オンライン開催の発表資料です。

mitti1210

April 23, 2022
Tweet

More Decks by mitti1210

Other Decks in Programming

Transcript

  1. library(readxl) library(tidyverse) file <- "test.xlsx sheets <- excel_sheets(file) temp <-

    read_excel(file, sheet = 1, skip = 1) temp %>% filter(...1 == " ") %>% select(-1) %>% rowMeans(na.rm = TRUE) [1] 8.677419 1 ReadSheet <- function(file, sheet){ temp <- read_excel(file, sheet = sheet, skip = 1) mean <- temp %>% filter(...1 == " ") %>% select(-1) %>% rowMeans(na.rm = TRUE) year <- str_extract(sheet, "^20[0-9][0-9]") month <- str_extract(sheet,"[0-9]*$") result <- tibble( year = as.integer(year), momth = as.integer(month), value = mean ) return(result) } ReadSheet(file, sheets[1]) [1] 8.677419
  2. for (sheet in sheets) { tryCatch( { ReadSheet(file, sheet) #

    }, error = function(e){ print(paste0(sheet, )) # } ) } tryCatch
  3. ReadSheet <- function(file, sheet){ temp <- read_excel(file, sheet = sheet,

    skip = 1) mean <- temp %>% filter(...1 == " ") %>% select(-1) %>% mutate(across(everything(), as.numeric)) %>% # 1 rowMeans(na.rm = TRUE) year <- stringr::str_extract(sheet, "^20[0-9][0-9]") month <- stringr::str_extract(sheet,"[0-9]*$") result <- tibble( year = as.integer(year), momth = as.integer(month), value = mean ) return(result) } ReadSheet(file, sheets[7])