Reading Excel in R: how to find the start cell in messy spreadsheets

后端 未结 7 1701
暗喜
暗喜 2020-12-28 10:17

I\'m trying to write R code to read data from a mess of old spreadsheets. The exact location of the data varies from sheet to sheet: the only constant is that the first co

7条回答
  •  囚心锁ツ
    2020-12-28 10:29

    Okay, at the format was specified for xls, update from csv to the correctly suggested xls loading.

    library(readxl)
    data <- readxl::read_excel(".../sampleData.xls", col_types = FALSE)
    

    You would get something similar to:

    data <- structure(list(V1 = structure(c(6L, 5L, 3L, 7L, 1L, 4L, 2L), .Label = c("", 
    "Apr 14", "GROSS PERFROANCE DETAILS", "Mar-14", "MC Pension Fund", 
    "MY COMPANY PTY LTD", "updated by JS on 6/4/2017"), class = "factor"), 
        V2 = structure(c(1L, 1L, 1L, 1L, 4L, 3L, 2L), .Label = c("", 
        "0.069%", "0.907%", "Monthly return"), class = "factor")), .Names = c("V1", 
    "V2"), class = "data.frame", row.names = c(NA, -7L))
    

    then you can dynamincally filter on the "Monthly return" cell and identify your matrix.

    targetCell <- which(data == "Monthly return", arr.ind = T)
    returns <- data[(targetCell[1] + 1):nrow(data), (targetCell[2] - 1):targetCell[2]]
    

提交回复
热议问题