I have N data sets which were loaded into RStudio and stored in the list object "datasets". The problem is what I want to be the top row in each of them or the headers for each of them, either way is in their third rows.
The initial version of this question I posted only had the paragraph below describing what each of the N datasets look like, but I realized that is not nearly simple enough, so now I am including a screenshot of what one of them looks like right below that paragraph.
Each dataset is 503 by 31 and that 3rd row is "Y", "X1", "X2", ..., "X30" in every dataset, the first row in each of them is a row of dummy variables, so all of them are either 1 or 0 depending on a condition. The 2nd row of each is black in the first spot, then '1', '2', '3', ..., '30'.
What I want to do from here is to add a new row, one equivalent to the 3rd row, to the top of each of these N dataframe elements within the list object datasets, or add proper headers to them instead which would be even better. Or, find a way to make delete or drop the 2nd row, then make the 1st and the new 2nd row switch places.
I just also took the liberty of adding in that new row in the source csv file-formatted dataset and screenshotting that to demonstrate what the dataframe in R for that dataset should look like after I am done applying whatever answer to this question works
Would it be possible for me to somehow combine an rbind() function with one of the apply functions to accomplish this task??
p.s. What is below the 3rd rows of each dataframe are just 500 rows of observations on each of the 31 variables.
I already tried to add the aforementioned row names to each dataframe using the following:
lapply(datasets, function(i){
colnames(i) <- c("Y", "X1","X2", "X3", "X4","X5", "X6", "X7","X8", "X9",
"X10","X11", "X12", "X13","X14", "X15", "X16","X17",
"X18", "X19","X20", "X21", "X22","X23", "X24", "X25",
"X26", "X27", "X28","X29", "X30") }
But this didn't actually result in any permanent changes in datasets at all to my surprise.
p.s. The 2nd thing I do in this script (after setting the WorkSpace) is load the following libraries with the following unorthodox method:
# load all necessary packages using only 1 command/line
library_list <- c(library(stats),library(plyr),library(dplyr),
library(tidyverse),library(tibble),library(readr),
library(leaps),library(lars),library(stringi),
library(purrr),library(parallel), library(vroom))
I just run rm(library_list immediately) afterwards and it's like I never did it weird. I do it that way because my hands are disabled, so the less thumb clicks to run each line individually, the better!