[R] conditional output of string to file n times...

Chris Evans chr|@ho|d @end|ng |rom p@yctc@org
Wed Jul 7 13:10:42 CEST 2021


And just for the fun of it, a tidverse way.  I quite like uncount()

### get the library
library(tidyverse) # or just library(tidyr)

### create data as per nice base R e.g.
x <- data.frame(label = c("ABC","DDG","ABB"), count = c(3,5,2))
### use uncount to ... well, uncount it!
uncount(x, count) -> y
y

# or using tidyverse piping to do the same
x %>%
  uncount(., count) -> y

### and one of many ways to write to a simple file
### make sure you do this in a safe place!
cat(y, file = "safefile.txt", sep = "\n")


Cheers all,

Chris

----- Original Message -----
> From: "Rui Barradas" <ruipbarradas using sapo.pt>
> To: "Evan Cooch" <evan.cooch using gmail.com>, r-help using r-project.org
> Sent: Wednesday, 7 July, 2021 09:07:00
> Subject: Re: [R] conditional output of string to file n times...

> Hello,
> 
> Sorry, I forgot the output to file part.
> 
> 
> y <- rep(df1[[1]], df1[[2]])
> cat(y, file = "~/tmp/rhelp.txt", sep = "\n")
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Às 09:01 de 07/07/21, Rui Barradas escreveu:
>> Hello,
>> 
>> Use ?rep.
>> Since you say you have a file, in the code below I will read the data
>> from a connection. Then create the string.
>> 
>> 
>> txtfile <- "ABC 3
>> DDG 5
>> ABB 2"
>> 
>> tc <- textConnection(txtfile)
>> df1 <- read.table(tc)
>> close(tc)
>> 
>> rep(df1[[1]], df1[[2]])
>> #[1] "ABC" "ABC" "ABC" "DDG" "DDG" "DDG" "DDG" "DDG" "ABB" "ABB"
>> 
>> 
>> Hope this helps,
>> 
>> Rui Barradas
>> 
>> 
>> Às 14:27 de 06/07/21, Evan Cooch escreveu:
>>> Suppose I have a file with the the following structure - call the two
>>> space-separated fields 'label' and 'count':
>>>
>>> ABC 3
>>> DDG 5
>>> ABB 2
>>>
>>>
>>> What I need to do is parse each line of the file, and then depending
>>> on the value of count, write out the value of 'label' to a new file,
>>> but 'count' times. In other words, take the preceding, and output
>>>
>>> ABC
>>> ABC
>>> ABC
>>> DDG
>>> DDG
>>> DDG
>>> DDG
>>> DDG
>>> ABB
>>> ABB
>>>
>>> I was wondering if there was an elegant/simple way to do this? I can
>>> do this relatively easily in perl, or awk, but am stumped by getting a
>>> bit of R code to accomplish the same thing.
>>>
>>> Many thanks in advance...
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>> 
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Chris Evans (he/him) <chris using psyctc.org> Visiting Professor, University of Sheffield <chris.evans using sheffield.ac.uk>
I do some consultation work for the University of Roehampton <chris.evans using roehampton.ac.uk> and other places
but <chris using psyctc.org> remains my main Email address.  I have a work web site at:
   https://www.psyctc.org/psyctc/
and a site I manage for CORE and CORE system trust at:
   http://www.coresystemtrust.org.uk/
I have "semigrated" to France, see: 
   https://www.psyctc.org/pelerinage2016/semigrating-to-france/ 
   https://www.psyctc.org/pelerinage2016/register-to-get-updates-from-pelerinage2016/

If you want an Emeeting, I am trying to keep them to Thursdays and my diary is at:
   https://www.psyctc.org/pelerinage2016/ceworkdiary/
Beware: French time, generally an hour ahead of UK.



More information about the R-help mailing list