1

I am trying to create a dataframe containing made up sentences. In order to make the sentences vary I want to sometimes concatenate a string to a pre-existing string and sometimes not

Example input

FD1<-data.frame(c("It is raining","It is snowing","It is stormy")) FD_try<-list(x="a lot",x="a bit") It is raining It is snowing It is stormy 

Example output 1

It is raining a lot It is snowing It is stormy 

Example output 1

It is raining It is snowing a lot It is stormy a bit 

I am currently doing

out <- apply(FD1, 1, function(x) { return(paste(x,sample(FD_try,1,replace=F))) }) 

but that always inserts from FD_try. How can I make it insert only sometimes?

2 Answers 2

2

Perhaps this helps

trimws(paste(FD1[,1], c("", unlist(FD_try)))) 
Sign up to request clarification or add additional context in comments.

3 Comments

...One of those 'why didn't I think of that' moments
...but if there also a way of sampling so that if I wanted I could optionally add two of the elements in FD_try to get for example It is raining a lot a bit
@SebastianZeki You can concatenate in that list and do a sample on it
1

You can do:

FD1 <- data.frame(x=c("It is raining", "It is snowing", "It is stormy")) FD_try <- c("a lot", "a bit", "") FD1$new <- paste0(FD1$x, sample(FD_try, nrow(FD1), repl=TRUE)) FD1 

You can use replace=FALSE only if nrow(FD1)<=length(FD_try)

2 Comments

so the replace =T makes it optional. Got it.
If you want to use replace=FALSE, there must nrow(FD1)<=length(FD_try)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.