I have the following dataframe:
Name Occupation Country code Remarks
Mark Engineer 1 Ok
Jerry Engineer 1 None
Marie Veterinarian 2 Ok
Nolan Veterinarian 2 Ok
Max Shepherd 2 Ok
I want to output the data frame into .txt files using the following codes:
df1 <- structure(list(Name = c("Mark", "Jerry", "Marie", "Nolan", "Max"),
Occupation = c("Engineer", "Engineer", "Veterinarian",
"Veterinarian", "Shepherd"),
Countrycode = c(1L, 1L, 2L, 2L, 2L),
Remarks = c("Ok", "None", "Ok", "Ok", "Ok")),
class = "data.frame", row.names = c(NA, -5L))
df2 <- transform(df1, NameRemarks = paste(Name, Remarks, sep=" - "))
[, c("NameRemarks", "Occupation", "Countrycode")]
lst1 <- lapply(split(df2[-3], df2$Countrycode),
function(x) split(x['NameRemarks'], x$Occupation))
Map(capture.output, lst1, file = paste0("output", seq_along(lst1), ".txt"))
However, the output2.txt displays:
$Shepherd
NameRemarks
5 Max - Ok
$Veterinarian
NameRemarks
3 Marie - Ok
4 Nolan - Ok
This is also the case with output1.txt. I want to clean the formatting into:
Shepherd
Max - Ok
Veterinarian
Marie - Ok
Nolan - Ok

