Using R to repeatedly replace words

Head and toes

I have a sentence and I printed it in two lines:

cat("Today is Monday and it is sunny.\nMonday is a sunny day\n")

and I have two lists:

day<-c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
weather<-c("sunny", "cloudy", "rainy", "stormy","sunny", "cloudy", "rainy")

I am trying to produce a text file which would contain the string variable above but with every day and weather variable in the lists.

So I would have:

Today is Monday and it is sunny.
Monday is a sunny day
Today is Tuesday and it is cloudy.
Tuesday is a cloudy day
Today is Wednesday and it is rainy.
Wednesdady is a rainy day   

.... and so on.

I am wondering if this is possible in R. I am a relatively new beginner of R so any explanation would be much appreciated!!

Alex A.

You could construct a simple loop and print using cat.

for (i in 1:length(day)) {
    cat("Today is", day[i], "and it is a", weather[i], "day.\n")
    cat(day[i], "is a", weather[i], "day\n")
}

# Today is Monday and it is a sunny day.
# Monday is a sunny day
# Today is Tuesday and it is a cloudy day.
# Tuesday is a cloudy day
# Today is Wednesday and it is a rainy day.
# Wednesday is a rainy day
# Today is Thursday and it is a stormy day.
# Thursday is a stormy day
# Today is Friday and it is a sunny day.
# Friday is a sunny day
# Today is Saturday and it is a cloudy day.
# Saturday is a cloudy day
# Today is Sunday and it is a rainy day.
# Sunday is a rainy day

This loops through all elements of your vector and it accesses each day and weather based on the index.

Day: "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday"
Index:  1         2          3           4         5         6        7

At the first iteration of the loop, i is set to 1, and day[i] is "Monday". Likewise, weather[i] is "sunny" since "sunny" is the first element in the weather vector. That pattern is repeated over the whole vector.

So at the first iteration, the first sentence is

cat("Today is", "Monday", "and it is a", "sunny", "day.\n")

By default, cat separates consecutive values with a space. So this prints exactly the sentence we're looking for. The same is done for the second sentence.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using R to repeatedly replace words

From Dev

R: Replace Abbreviations\ Words

From Dev

Replace words in R

From Dev

Using sed to replace words

From Dev

Replace words in a text using python

From Dev

Replace string repeatedly

From Dev

Replace a list of words occuring in sentences in R

From Dev

Find and replace words in a text file using java

From Dev

using regex to replace words not enclosed in <> signs

From Dev

Replace letters with Words using JavaScript Regex

From Dev

How to replace short words into full words from tweets using python

From Dev

pandas string replace TypeError - replace words using pandas df column

From Dev

Replace all the first character of words in a string using preg_replace()

From Dev

Replace two "words" in a string using str_replace

From Dev

R: Defining a function (and/or using apply() or for loop) to perform a set of procedures repeatedly

From Dev

Replace all words ending with n't in a data-frame in R

From Dev

Replace brackets around words with something else using regex in Python

From Dev

How to replace a words near the specific word in a text file using python

From Dev

Replace certain arabic words in text string using Javascript

From Dev

Issues with replacing words in a string using a dictionary and the replace() function

From Dev

Replace brackets around words with something else using regex in Python

From Dev

MS Excel: How to insert words in cells using Find and Replace

From Dev

How to remove stop words using string.replace()

From Dev

How to replace words using wildcards in MS Word 2013?

From Dev

Replace multiple occurrences of word with words from list using sed

From Dev

How can search and replace words but with confirmation using vi

From Dev

Replace multiple words from sentence in textarea using javascript

From Dev

R tm substitute words in Corpus using gsub

From Dev

Using R to find top ten words in a text

Related Related

  1. 1

    Using R to repeatedly replace words

  2. 2

    R: Replace Abbreviations\ Words

  3. 3

    Replace words in R

  4. 4

    Using sed to replace words

  5. 5

    Replace words in a text using python

  6. 6

    Replace string repeatedly

  7. 7

    Replace a list of words occuring in sentences in R

  8. 8

    Find and replace words in a text file using java

  9. 9

    using regex to replace words not enclosed in <> signs

  10. 10

    Replace letters with Words using JavaScript Regex

  11. 11

    How to replace short words into full words from tweets using python

  12. 12

    pandas string replace TypeError - replace words using pandas df column

  13. 13

    Replace all the first character of words in a string using preg_replace()

  14. 14

    Replace two "words" in a string using str_replace

  15. 15

    R: Defining a function (and/or using apply() or for loop) to perform a set of procedures repeatedly

  16. 16

    Replace all words ending with n't in a data-frame in R

  17. 17

    Replace brackets around words with something else using regex in Python

  18. 18

    How to replace a words near the specific word in a text file using python

  19. 19

    Replace certain arabic words in text string using Javascript

  20. 20

    Issues with replacing words in a string using a dictionary and the replace() function

  21. 21

    Replace brackets around words with something else using regex in Python

  22. 22

    MS Excel: How to insert words in cells using Find and Replace

  23. 23

    How to remove stop words using string.replace()

  24. 24

    How to replace words using wildcards in MS Word 2013?

  25. 25

    Replace multiple occurrences of word with words from list using sed

  26. 26

    How can search and replace words but with confirmation using vi

  27. 27

    Replace multiple words from sentence in textarea using javascript

  28. 28

    R tm substitute words in Corpus using gsub

  29. 29

    Using R to find top ten words in a text

HotTag

Archive