1
votes

My code is like this

 if (startsWith(i, "CombinedReport_2")) {
    temp1 <- read.csv(paste(directory, i, sep = ""), header = TRUE)
    if (ncol(temp1) < 19) {
      names(temp1) <- dfCombinedNamesOld
    }
    else {
      names(temp1) <- names(dfCombined)    `dfcombined and fdcombinedold are empty dataframes with headers
    }
    dfCombined <- bind_rows(dfCombined,temp1)
 }}

This is strange because i have about 11 files in the directory that it's trying to merge into dfCombined and the structure for all of them is like this:

'data.frame':   7681 obs. of  19 variables:
 $ ï..Activity.ID         : Factor w/ 2938 levels "CBCC-1E1EELE",..: 23 23 23 39 39 39 39 39 39 39 ...
 $ Type                   : Factor w/ 5 levels "Attendee List",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ Activity.Subtype       : Factor w/ 14 levels "1.  IMW â\200“ Introduction",..: 5 5 5 4 4 4 4 4 4 4 ...
 $ Created.Date           : Factor w/ 2936 levels "2019-01-01 23:27:54",..: 23 23 23 40 40 40 40 40 40 40 ...
 $ Due.Date               : Factor w/ 83 levels "2019-08-01 00:00:00",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Modified.By            : Factor w/ 55 levels "imrankhan","imrankhan",..: 10 10 10 38 38 38 38 38 38 38 ...
 $ Modified               : Factor w/ 2851 levels "2019-06-27 11:35:59",..: 538 538 538 331 331 331 331 331 331 331 ...
 $ Completed.Date         : Factor w/ 2001 levels "","2019-06-27 11:35:58",..: 19 19 19 10 10 10 10 10 10 10 ...
 $ Subject                : Factor w/ 1319 levels "$2 Million Opp",..: 1167 1167 1167 644 644 644 644 644 644 644 ...
 $ Fiscal_Quarter         : int  4 4 4 4 4 4 4 4 4 4 ...
 $ Fiscal_Year            : int  2019 2019 2019 2019 2019 2019 2019 2019 2019 2019 ...
 $ Expense.Type           : Factor w/ 12 levels "","Movie",..: 9 9 9 9 9 9 9 9 9 9 ...
 $ Expense.Amount         : num  211 211 211 581 581 ...
 $ Cost.per.Contact       : num  52.8 52.8 52.8 23.2 23.2 ...
 $ Contact.ID             : Factor w/ 4705 levels "CBCC-1E1EELE",..: 2032 2825 3365 77 334 1018 1406 1593 2350 2430 ...
 $ Full.Name              : Factor w/ 4676 levels "Alex Khong",..: 4073 2896 3979 142 3587 1695 115 2798 280 3942 ...
 $ Branch                 : Factor w/ 1705 levels "","100 MILE HOUSE-9962-001041",..: 353 363 353 117 117 117 117 117 117 117 ...
 $ Primary Code: Factor w/ 3406 levels "","-","7584-0858",..: 2239 1 1 1 1 1 980 973 983 982 ...
 $ Sub.Region             : Factor w/ 38 levels "CF","DR","EC51",..: 31 31 31 3 3 3 3 3 3 3 ...

Why is bind_rows throwing an error ```Error: Can't combine ..1$Activity ID and ..2$Activity ID <factor<2585d>>.

2
Can you convert the columns that are factor to character. - akrun
I tried something like this, bind_rows(mutate_all(dfCombined, as.character), mutate_all(temp1, as.character)). The error it threw was Error: Only strings can be converted to symbols - Piccinin1992
Try dfCombined %>% mutate(across(where(is.factor), as.character)) - akrun
Would the expression be temp1<-temp1 %>% (mutate(across(where(is.factor), as.character))) + dfcombined<-bind_rows(dfCombined,temp1) - Piccinin1992
Please check if the first expression works or not temp1 <- temp1 %>% . - akrun

2 Answers

2
votes

While inflexible, i went ahead with rbind which solved the problem for now. Still not able to figure out what caused Bind_rows to fail.

0
votes

It may be easier to do with map

library(purrr)
library(readr)     
files <- list.files( path = directory, pattern = "*.csv", 
            full.names = TRUE, recursive = FALSE )
files1 <- files[startsWith(files, "CombinedReport_2")]
out <- map_dfr(files1, read_csv)

If we need to create a condition for naming

out <- map_dfr(files, ~
                       { 
                       temp <- read_csv(.x)
                       names(temp) <- if(ncol(temp) < 19) {
                                      dfCombinedNamesOld
                                  } else names(dfCombined)
                         temp
                          })