The paste function R in dplyr does not execute as expected

I am confused about how the paste () function works. I have a dplyr table with the following columns:

Year    Month    DayofMonth
2001    May      21
2001    May      22
2001    June     9
2001    March    4

What I would like to combine in one column called "Date". I realized that I used the command:

df2 = mutate(df, Date = paste(c(Year, Month, DayofMonth), sep = "-",))

Unfortunately, this seems to combine every element in the year, then every element in the month, then every element in DayofMonth, so the result looks something like this:

2001-2001-2001-2001 ... May-May-June-March ... 21-22-9-4

How can I change my command so that the insert function repeats on each line separately?

PS This is part of the Data Camp course , and so I run the commands through any version of R that they received on their server.

+4
source share
1

. c() paste(), .

mutate(df, Date = paste(Year, Month, DayofMonth, sep = "-"))
#   Year Month DayofMonth         Date
# 1 2001   May         21  2001-May-21
# 2 2001   May         22  2001-May-22
# 3 2001  June          9  2001-June-9
# 4 2001 March          4 2001-March-4
+6

Source: https://habr.com/ru/post/1614505/


All Articles