I'm having problems to save the output of a process back to csv.
I originally had two csv files containing email addresses. I used the following code to find the nearest match for each email from the first csv within the second csv.
DATA:
---dat1.csv------------------------------
Email,FullName
msmith@example.com.au,Matthew Smith
sprakash@example.com.au,Ishaan Prakash
mkumar@example.com.au,Menab Kumar
kwilliams@example.com.au,Kathryn Williams
cphilip@example.com.au,Christa Philip
ldavies@example.com.au,Lewis Davies
------------------------------------------
---dat2.csv------------------------------
Email,FullName
msmth@example.ogr.au,Matt Smith
sprakash@example.org,Ish Prakash
mkmr@example.com.au,Menab Kumar
kwilliamsATexample.com.au,Kat Williams
cjphilip@example.com.au,Christa J. Philip
ldav@example.com.au,Lewis Gerard Davies
------------------------------------------
library(tidyverse)
sfdc_dat <- read_csv("dat1.csv")
sfmc_dat <- read_csv("dat2.csv")
sfmc_dat$Email_SFDC <- sapply(sfmc_dat$Email,
function(x){
char_min <- stringdist::stringdist(x, sfdc_dat$EMAIL)
sfdc_dat[which.min(char_min), "EMAIL"]
})
head(sfmc_dat)
OUTPUT:
FullName | Email_SFDC | |
---|---|---|
<chr> | <chr> | <named list> |
msmth@example.ogr.au | Matt Smith | msmith@example.com.au |
sprakash@example.org | Ish Prakash | sprakash@example.com.au |
mkmr@example.com.au | Menab Kumar | mkumar@example.com.au |
kwilliamsATexample.com.au | Kat Williams | kwilliams@example.com.au |
cjphilip@example.com.au | Christa J. Philip | cphilip@example.com.au |
ldav@example.com.au | Lewis Gerard Davies | ldavies@example.com.au |
The problem is that I ended up with a tibble, where Email_SFDC is a Named List.
When I tried to write my data back to a csv, it saves the file and creates the column Email_SFDC, but the column is empty, while in the tibble is not empty.
write_csv(sfmc_dat, 'File_Name.csv')
FullName | Email_SFDC | |
---|---|---|
mbea@example.ogr.au | Matt Smith | |
sprakash@example.org | Ish Prakash | |
mkmr@example.com.au | Menab Kumar | |
kwilliamsATexample.com.au | Kat Williams | |
cphilip@example.com.au | Christa J. Philip | |
ldavies@example.com.au | Lewis Gerard Davies |
If I try
write.csv(sfmc_dat , 'File_Name.csv')
Error in utils::write.table(sfmc_dat, "File_Name2.csv", col.names = NA, : unimplemented type 'list' in 'EncodeElement'
Traceback:
1. write.csv(sfmc_dat, "File_Name2.csv")
2. eval.parent(Call)
3. eval(expr, p)
4. eval(expr, p)
5. utils::write.table(sfmc_dat, "File_Name2.csv", col.names = NA,
. sep = ",", dec = ".", qmethod = "double")
It took me a whole weekend to process the data and I need to rescue my output, so re-writing and re-running my code is not an option at this point. I need to convert the Named List column to a Chr type.
I will appreciate any suggestions