mitti1210
April 23, 2022
380

# 前処理R 第2回資料

April 23, 2022

## Transcript

7. ### 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

9. ### for (sheet in sheets) { tryCatch( { ReadSheet(file, sheet) #

}, error = function(e){ print(paste0(sheet, )) # } ) } tryCatch

11. ### 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])