【问题标题】:How to apply a function over multiple excel sheets in R?如何在R中的多个Excel工作表上应用一个函数?
【发布时间】:2020-04-09 06:41:44
【问题描述】:

我有一个excel 和两个sheets,我想将我创建的function 应用到两个sheets。我所做的是使这些工作表成为listdata frames。我已经创建了我的function,但它似乎不起作用。我关注了这个tutorial。这是一个可重现的例子。

structure(list(Date = structure(c(1416182400, 1416873600, 1417564800, 
1418256000, 1418947200, 1419638400, 1420070400, 1420761600, 1421452800, 
1422144000, 1422835200, 1423526400, 1424217600, 1424908800, 1425600000, 
1426291200, 1426982400, 1427673600, 1428364800, 1429056000, 1429747200, 
1430438400, 1431129600, 1431820800, 1432512000, 1433203200, 1433894400, 
1434585600, 1435276800, 1435968000, 1436659200), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), DOY = c("321", "329", "337", "345", 
"353", "361", "001", "009", "017", "025", "033", "041", "049", 
"057", "065", "073", "081", "089", "097", "105", "113", "121", 
"129", "137", "145", "153", "161", "169", "177", "185", "193"
), `1556_7S_15D` = c("4", "2", "4", "4", "4", "4", "4", "3", 
"3", "4", "NA", "NA", "4", "4", "4", "4", "3", "4", "4", "4", 
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4"), `2216_7S_15D` = c("3", 
"3", "3", "3", "3", "3", "2", "3", "NA", "3", "NA", "NA", "3", 
"NA", "NA", "1", "NA", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3"), `3220_7S_15D` = c("6", "1", "6", 
"6", "6", "6", "6", "6", "6", "6", "2", "5", "6", "6", "6", "6", 
"NA", "6", "6", "6", "NA", "6", "6", "6", "6", "6", "6", "6", 
"6", "6", "6"), `3343_7S_15D` = c("4", "NA", "4", "4", "4", "4", 
"4", "4", "2", "4", "NA", "NA", "4", "4", "4", "2", "NA", "4", 
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4"
), `4168_7S_15D` = c("2", "4", "4", "4", "4", "4", "NA", "4", 
"NA", "4", "NA", "NA", "4", "4", "4", "4", "NA", "4", "4", "4", 
"NA", "4", "3", "4", "4", "4", "4", "4", "4", "4", "4"), `4303_7S_15D` = c("4", 
"4", "4", "NA", "4", "4", "4", "4", "NA", "4", "NA", "4", "4", 
"4", "NA", "NA", "NA", "4", "4", "4", "4", "4", "4", "4", "4", 
"4", "4", "4", "4", "4", "4"), `4304_7S_15D` = c("4", "4", "4", 
"NA", "4", "4", "4", "4", "NA", "4", "NA", "4", "4", "4", "NA", 
"NA", "NA", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
"4", "4", "4", "4"), `4307_7S_15D` = c("3", "2", "3", "NA", "3", 
"3", "3", "3", "3", "3", "3", "NA", "3", "3", "NA", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3"), `4556_7S_15D` = c("3", "NA", "3", "3", "NA", "3", "3", 
"3", "NA", "3", "3", "2", "3", "3", "NA", "NA", "3", "3", "3", 
"3", "2", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3"), 
    `4573_7S_15D` = c("4", "NA", "4", "4", "NA", "4", "4", "4", 
    "4", "4", "NA", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
    "2", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4"), DOY_SEQ = c(26, 
    27, 28, 29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
    13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25)), row.names = c(NA, 
-31L), class = c("tbl_df", "tbl", "data.frame"))

我目前使用的代码是:

df = read_excel("test.xlsx")
colnames(df)

path <- "test.xlsx"

#get the sheet names
path %>%
  excel_sheets()

#transform the sheets into a list of data frames
df <- path %>%
  excel_sheets() %>%
  set_names() %>%
  map(read_excel,
      path = path)

str(df)

cleaner <- function(df){
  # drop 1st rows of each sheet
  df <- subset(df, select = -c(Date))
  # transform all cells from char to num 
  df[] <- lapply(df, as.numeric)
  # interpolation using splines
  df[] <- lapply(df, zoo::na.spline)
  # return cleaned df      
  return(df)
}
str(df)

【问题讨论】:

  • 如果df 有数据框列表,请执行lapply(df, cleaner)
  • 感谢您的快速回复。你的意思是在function之后插入你的代码吗?
  • 是的,你试过了吗?
  • 我做了,但仍然没有结果。例如cells 的类型仍然是character 值。
  • df[] &lt;- lapply(df, cleaner)

标签: r excel function


【解决方案1】:

我们可以使用lapply 循环遍历数据帧列表并将cleaner 函数应用于每个数据帧。

df[] <- lapply(df, cleaner)

同样,使用purrr::map

df[] <- purrr::map(df, cleaner)

【讨论】:

    【解决方案2】:

    我们也可以使用

     library(purrr)
     map_dfr(df, cleaner)
    

    【讨论】:

      猜你喜欢
      • 2015-10-07
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 2021-08-09
      • 2018-07-09
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      相关资源
      最近更新 更多