1

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 create one text file per country, say:

output1.txt:

 Engineer
 Mark - Ok
 Jerry - None

output2.txt:

 Veterinarian
 Marie - Ok
 Nolan - Ok

 Shepherd
 Max - Ok

2 Answers 2

1

We can do a split in. base R into a list of vectors

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))
#$`1`
#$`1`$Engineer
#   NameRemarks
#1    Mark - Ok
#2 Jerry - None


#$`2`
#$`2`$Shepherd
#  NameRemarks
#5    Max - Ok

#$`2`$Veterinarian
#  NameRemarks
#3  Marie - Ok
#4  Nolan - Ok

The format may not fit well in writing the file. One option is capture.output

Map(capture.output, lst1, file = paste0("output", seq_along(lst1), ".txt"))

-output

enter image description here

For storing purpose, it may be better to do a single split

lst1 <- split(df2[-3], df2$Countrycode)
lapply(names(lst1), function(x) write.csv(lst1[[x]], 
   file = paste0("output", x, ".csv"), row.names = FALSE, quote = FALSE))

Or another option is tidyverse

library(dplyr)
library(tidyr)
library(stringr)
df1 %>%        
    unite(NameRemarks, Name, Remarks, sep= " - ") %>%
    group_by(Countrycode) %>%
    mutate(rn = row_number()) %>%
    ungroup %>%
    pivot_wider(names_from = Occupation, values_from = NameRemarks)
# A tibble: 5 x 5
#  Countrycode    rn Engineer     Veterinarian Shepherd
#        <int> <int> <chr>        <chr>        <chr>   
#1           1     1 Mark - Ok    <NA>         <NA>    
#2           1     2 Jerry - None <NA>         <NA>    
#3           2     1 <NA>         Marie - Ok   <NA>    
#4           2     2 <NA>         Nolan - Ok   <NA>    
#5           2     3 <NA>         <NA>         Max - Ok

data

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))
Sign up to request clarification or add additional context in comments.

10 Comments

how to output one text per country? @akrun
I would like to use only base functions @akrun
Yes. I want to split with the Occupation but I want to create separate text files per Country.code @akrun
split(with(df1, paste(Name, Remarks, sep=" - ")), df1$Occupation) is OK but I want to create separate text files for every country. I try using write.table() @akrun
@KylaCelineMarcaida ssorry, need to remov e the MoreArgs updated it shsould work now
|
1

Another option is iterate making named .csv files with purrr:


library(tidyverse)

# Example data
df <- tibble(name = c('Mark', 'Jerry', 'Marie', 'Nolan', 'Max'),
             occupation = rep(c('engineer', 'vetenarian', 'shepher'), c(2, 2, 1)),
             country_code = rep(c(1, 2), c(3, 2)),
             remarks = c('ok', 'none', 'ok', 'ok', 'ok'))

# Get a list of all occupations as a vector
occupations <- df$occupation %>% unique()

# Create a list of subdataframes split by occupation
dfs <- map(occupations, ~ df %>%
      filter(occupation == paste(.x)))

# Create .csv files from your dataframe list in your 
# working directory: "engineers.csv", "veterinarians.csv", ...
walk2(dfs, occupations, ~ write_csv(.x, paste0(.y, 's.csv')))


6 Comments

I only want the Name - Remarks columns in the output @Adam B.
And the output should be a .txt file not a .csv @Adam B.
You can easily select only Name + Remarks by adding a pipe with select() into the map() call: dfs <- map(occupations, ~ df %>% filter(occupation == paste(.x)) %>% select(name, remarks))
You sure you want a .txt file? You can open a .csv file with any text editor, it will look the same like .txt if you open it in Notepad, and if you need to work with it again, you can always easily load it back into R or any other statistical software. However, if you save it as .txt, it might make it difficult to load it.
I need base R functions only. And yes, I need it in a .txt format @Adam B.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.