just got into data analysis, and this is my first post.
I have 12 dataframes in R that I need to first subset from, then add a bunch of columns to. My current solution is to just copy/paste the same code chunk over and over and edit the numbers for each corresponding data frame, but that's obviously inefficient and not sustainable for future practice.
This is how I'm subsetting the data currently:
sub_202212 <- subset(
cyclistic202212,
select = c(ride_id, rideable_type, started_at, ended_at, member_casual))
And this is currently how I'm adding columns:
sub_202202$month <- format(as.Date(sub_202202$date), "%m")
sub_202202$day <- format(as.Date(sub_202202$date), "%d")
sub_202202$year <- format(as.Date(sub_202202$date), "%Y")
sub_202202$day_of_week <- format(as.Date(sub_202202$date), "%A")
I'm wondering if there's a "batch process" that can drastically shorten my code.
Better yet, please let me know if there's also a way to subset columns while adding them at the same time (since it's the same addition/subtraction across all DFs).
And if a similar question has been asked (but worded better, which could be why I haven't found it), then please point me in that direction. This is all very new to me, and I still have a lot to learn.
Any insight would be greatly appreciated. :)
edit: sample code
df1 <- data.frame(id = c(1, 2, 3, 4, 5),
trips = c(3, 6, 3, 7, 8))
df2 <- data.frame(id = c(6, 7, 8, 9, 10),
trips = c(3, 5, 2, 7, 10))