1
votes

I am trying to script R to take the output of lapply and export it as a .csv with the following header: score, file.

This is how I have imported the files and created a corpus of .txt files:

folder <- "C:\\Users\\super\\Documents\\Mette\\data3\\bla"
filelist <- list.files(path=folder, pattern="*.txt")
files <- lapply(filelist, FUN=readLines, encoding = "UTF-8")
corpus4 <- lapply(files, FUN=paste, collapse=" ")

I am running this lapply function over the corpus of .txt files i created above:

library(Sentida)
lapply(corpus4, sentida, output = "mean") 

This produces a list of scores that look like this in the console:

[[1]]

[1] 0.1517111

[[2]]

[1] 0.4068402

[[3]]

[1] 0.3138707

Now I want to export/print this list to a .csv file that lists the scores AND their corresponding file name. Ideally, I want the .csv to look like this:

score, file
0.1517111, file1.txt
0.4068402, file2.txt
0.3138707, file3.txt

I have tried working with write.csv but I have a hard time getting the .csv in the format mentioned above. Any help would be greatly appreciated!

1

1 Answers

1
votes
score = unlist(lapply(corpus4, sentida, output = "mean"))
DF = data.frame(score,  filelist)
write.csv(DF, "Scores.csv")