It is frustrating to see your code choke part of the way via although attempting to utilize a operate in R. You could know that a little something in one of these objects prompted a difficulty, but how do you monitor down the offender?

The purrr package’s potentially() operate is one straightforward way.

In this case in point, I’ll demo code that imports many CSV documents. Most files’ value columns import as figures, but one of these comes in as quantities. Running a operate that expects figures as enter will result in an mistake.

For setup, the code below loads numerous libraries I need and then works by using base R’s listing.documents() operate to return a sorted vector with names of all the documents in my data listing. 

library(purrr)
library(readr)
library(rio)
library(dplyr)
my_data_documents <- list.files("data_files", full.names = TRUE) {36a394957233d72e39ae9c6059652940c987f134ee85c6741bc5f1e7246491e6}>{36a394957233d72e39ae9c6059652940c987f134ee85c6741bc5f1e7246491e6}
sort()

I can then import the very first file and appear at its composition. 

x <- rio::import("data_files/file1.csv")
str(x)
'data.frame':	3 obs. of  3 variables:
 $ Category     : chr  "A" "B" "C"
 $ Value        : chr  "$4,256.48 " "$438.22" "$945.12"
 $ MonthStarting: chr  "12/1/20" "12/1/20" "12/1/20"

The two the Value and Month columns are importing as character strings. What I finally want is Value as quantities and MonthStarting as dates. 

Copyright © 2020 IDG Communications, Inc.