简体   繁体   中英

Create a user generated function in R which creates a new column of dates based on values from other columns

Let's say these are the first few columns of my dataset:

library(tidyverse)

df <- tibble(Year = rep(2020, times = 5),
             Month = seq(1:5),
             DayOfMonth = seq(1:5),
             DayOfWeek = seq(1:5))

# A tibble: 5 x 4
   Year Month DayOfMonth DayOfWeek
  <dbl> <int>      <int>     <int>
1  2020     1          1         1
2  2020     2          2         2
3  2020     3          3         3
4  2020     4          4         4
5  2020     5          5         5

At the moment the date is split into different columns as above. I would like to create a function that takes the value from each relevant column for each row in the dataset and adds a new column which combines those values into a yyyy/mm/dd format.

For example, I want the first row to have a corresponding value in a new "Date" column to be 2020/01/01 (same as 1 January 2020)

Since I'm new to RI don't have a great understanding of making functions myself so I'm struggling to find a starting point. Any help is much appreciated :)

this should do it

library(tidyverse)

df <- tibble(Year = rep(2020, times = 5),
             Month = seq(1:5),
             DayOfMonth = seq(1:5),
             DayOfWeek = seq(1:5))


out <-  apply(df, MARGIN = 1, paste, collapse="/")

[1] "2020/1/1/1" "2020/2/2/2" "2020/3/3/3" "2020/4/4/4" "2020/5/5/5"

apply with margin = 1 goes through each row and uses the paste function collapsing the row vector and places a "/" instead

Edit: whats up with the "DayOfWeek" maybe that should be removed, or just run apply(df[,1:3], MARGIN = 1, paste,collapse="/")

out <-  apply(df[,1:3], MARGIN = 1, paste,collapse="/")
test <- as.Date(out)

test
[1] "2020-01-01" "2020-02-02" "2020-03-03" "2020-04-04" "2020-05-05"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM