0

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'. enter image description here

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 enter image description here

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!

Marlen
  • 171
  • 11

1 Answers1

1

If I understand you correctly this should work:


library(janitor)
library(purrr)
library(dplyr)

# create a list

df1 <- read.table(header = FALSE, 
           text = '
           1 0 1 1 0
           1 2 3 4 5
           X1 X2 X3 X4 X5
           no no no no no')

df2 <- read.table(header = FALSE, 
                  text = '
           1 1 0 0 0
           6 7 8 9 10
           X1 X2 X3 X4 X5
           no no no no no')


my_list <- list(df1, df2)

Base R

# create a custom function and then use it with lapply
my_renamer <- function(df, row=3){
  names(df) <- df[row,]
  df
}

lapply(my_list, function(x) my_renamer(x, 3))

OR with purrr and janitors row_to_names:

map(my_list, ~row_to_names(., remove_rows_above = FALSE, 
                           remove_row = FALSE, 3))

OR with lapply and janitor:

lapply(my_list, function(x) row_to_names(x, remove_rows_above = FALSE, 3))
[[1]]
  X1 X2 X3 X4 X5
1  1  0  1  1  0
2  1  2  3  4  5
3 X1 X2 X3 X4 X5
4 no no no no no

[[2]]
  X1 X2 X3 X4 X5
1  1  1  0  0  0
2  6  7  8  9 10
3 X1 X2 X3 X4 X5
4 no no no no no
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    ahhh before I spend the 5-15 minutes reading through your proposed solution and then trying it out, I'll thank you up front for reminding me to include which libraries I have already loaded in a post like this to show that I have no phobia or intimidation about using any external packages if they help! – Marlen Dec 31 '22 at 08:55
  • I just added some MUCH needed extra information which I should have included initially to be honest. Hope it helps clarify what I need and what I am looking for. – Marlen Dec 31 '22 at 09:47
  • 1
    Exactly what your desired output look like should be done by the code I provided. – TarJae Dec 31 '22 at 13:03
  • 1
    fantastic! I am glad to hear that and I'll let you know if it works later. About 13 hours ago, I finally closed my eyes & held my breath and hit run on the line in my R script that loads all of the 260k sample datasets into the Workspace and my laptop has been trying to get that done very loudly & warmly since then and it's still going right now!! – Marlen Dec 31 '22 at 16:16