5

These reports are coming from quickbooks, downloaded as Excel files. Notice that the left column is this nested hierarchy based on the left spacing.

I need to separate Description column into separate columns based on the number of leading spaces on the left.

As I've been working with financial reports recently, these are super common and extremely difficult to work with. Is there a package or function for importing this type of data?

enter image description here

Here is example reproducible input dataframe:

df1 <- structure(list(Description = c("asset", " current asset", "   bank acc", 
                                      "    banner", "    clearing",
                                      "   total bank accounts",
                                      " total current assets"),
                 Total = c(NA, NA, NA, 10L, 20L, 30L, 30L)),
            .Names = c("Description", "Total"), 
            class = "data.frame", 
            row.names = c(NA, -7L))
3
  • 1
    "[leading indentations to signify multiple column hierarchy] are super common and extremely difficult to work with. Is there a package or function..." Not for the indentation thing apparently, but you could raise an enhance request on tidyxl.
    – smci
    Aug 16, 2018 at 1:40
  • 1
    @smci Good idea, posted a feature request
    – zx8754
    Aug 16, 2018 at 6:22
  • You probably need to restate the request in their github ticket; also to say that it occurs often in financial reports (Quickbooks? which other formats?). By the way, should the inferred columns be Col1,Col2... or do you want to give them names?
    – smci
    Aug 16, 2018 at 6:27

2 Answers 2

5

You can try tidyxl and unpivotr for these Excel wrangling tasks. Here are the docs:

Here's a nice tutorial: https://blog.davisvaughan.com/2018/02/16/tidying-excel-cash-flow-spreadsheets-in-r/

1
  • Thank you, Matt!!! I could not figure this one out for a week. Really appreciate it.
    – Super_John
    Aug 10, 2018 at 19:49
3

I think the real question is:

  • "How do I treat number of leading spaces to indicate nth column?"

If so, then try this example, code could be improved, but the idea is every leading space indicates nth column.

# example input, we will have similar input after reading in
# the Excel sheet into R.
df1 <- data.frame(x = c("x1", " x2", " x2", "  x3", "x1", " x2"),
                  y = c(NA,      22,    33,      44,   55,   66),
                  stringsAsFactors = FALSE)

library(dplyr)

cbind(
  bind_rows(
  lapply(df1$x, function(i){
    x <- data.frame(t(strsplit(i, split = " ")[[1]]), stringsAsFactors = FALSE)
    colnames(x) <- paste0("col", 1:ncol(x))
    x
    })
  ),
  df1[, "y", drop = FALSE])

#   col1 col2 col3  y
# 1   x1 <NA> <NA> NA
# 2        x2 <NA> 22
# 3        x2 <NA> 33
# 4             x3 44
# 5   x1 <NA> <NA> 55
# 6        x2 <NA> 66
2
  • I think you're completely right. Still have to figure out whether the above mentioned packages tackle this directly, otherwise, this is a great solution. Thank you so much!!!
    – Super_John
    Aug 13, 2018 at 22:08
  • @Super_John Great, that is what I thought. I edited your post with simplified reproducible input example. Hope that will attract better answers.
    – zx8754
    Aug 14, 2018 at 6:53

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.