[R] conditional output of string to file n times...
er|cjberger @end|ng |rom gm@||@com
Wed Jul 7 09:53:53 CEST 2021
I assume you know how to get the data into a data frame (e.g. via read.csv).
Here I will create the example data explicitly, creating a data frame x.
x <- data.frame( label=c("ABC","DDG","ABB"), count=c(3,5,2) )
Then create a character vector with the data as you want it.
y <- unlist(sapply( 1:nrow(x), function(i) rep( x$label[i], x$count[i] ) ))
Finally print it to a file, say 'myfile' (to get one element per line I did
a bit of a trick).
On Wed, Jul 7, 2021 at 10:15 AM Evan Cooch <evan.cooch using gmail.com> wrote:
> 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
> 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
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help