0

I have a set of 40 different datasets within a file folder which I have loaded into my WorkSpace in RStudio with:

datasets <- lapply(filepaths_list, read.csv, header = FALSE)

This object datasets is a list of 40 dataframes. I would like to run code which does the same as the following line of code does when the object is a single dataframe instead:

All_sample_obs <- datasets[-1:-3,]

From there, I would likewise like to find ways to find iterative versions of the following further 3 simple pre-processing transformation steps:

All_sample_obs <- lapply(All_sample_obs, as.numeric)
All_sample_obs <- as.data.frame(All_sample_obs)
All_sample_obs <- round(All_sample_obs, 3)
Marlen
  • 171
  • 11
  • 1
    You can do `all_sample_obs = lapply(all_sample_obs, function(x), x[-1:-3,])`. It is possible to convert them to numeric with `laplly(all_sample_obs, as.numeric)`. The step in which you convert the list to a data.frame, makes little sense, to me, **but I don't know the data**. Also, as you don't show us what is `df` is, then I can't tell you about the last block of code – PavoDive Dec 30 '22 at 13:58
  • @PavoDive all very helpful stuff, thank you. And, it's funny that you mentioned how the as.data.frame step looks unnecessary because it actually turns out to be in this case where the object is a list of loaded datasets, R automatically imports each element of that list as a dataframe, but this was not the case in my prototype script for a single dataset. In that case, it was a list or something else besides a dataframe, I am pretty sure it was a list though. – Marlen Dec 30 '22 at 14:55
  • @PavoDive the other quirky detail was that All_sample_obs <- lapply(All_sample_obs, as.numeric) is actually the command I used for the single dataset case, so I ended up using datasets <- lapply(All_sample_obs, \(X) { lapply(X, as.numeric) }) for the case in question here! – Marlen Dec 30 '22 at 14:57
  • @PavoDive looking back on my question with fresh eyes, I completely see what you mean about how the fact that I didn't include any printouts of how my data looks limits one's ability to answer my questions, I will add that on to the end in a PS section within the next few minutes. – Marlen Dec 31 '22 at 06:51

1 Answers1

0

The following should do what you are looking for:

All_sample_obs <- lapply(datasets, function(i) {i[-1:-3, ]})

All_sample_obs <- lapply(All_sample_obs, \(X) { lapply(X, as.numeric) })
All_sample_obs <- lapply(All_sample_obs, function(i) { as.data.frame(i) })
All_sample_obs <- lapply(All_sample_obs, \(X) { round(X, 3) })

I included two solutions with each of the two common syntaxes used for lapplies, both are valid.

Marlen
  • 171
  • 11