How can I extract a single element from a list into a data frame? - r

I compiled a list of ~60 data frames to keep my RStudio environment tidy.
I will need to occasionally extract a single element into a data frame so that I can work on it before putting it back into the list - how can this extract be achieved?
I am aware that I can manipulate the list element directly, but that isn't ideal and being able to extract the data frame would serve me better for my needs.

If dflist is your list of dataframes, then the easiest way to work on element n would be something like
df <- dflist[[n]]
#...work on df...then
dflist[[n]] <- df

Related

Pulling out elements of a list of tibbles after using dplyr group_split (R)

I'm working with a dataset containing the full works of Shakespeare, where each row in the dataframe is a line of a Shakespeare text, and am trying to combine the individual lines of text into one large row of content for each play of Shakespeare's. So far, I've been able to use group_split from dplyr to split the dataframe into tibbles containing the contents of each work, but I can't seem to pull the contents out of the tibbles based on the name of the play.
So far, I've been able to create a large vectors list of tibbles by using the following code:
playtbls <- data %>%
group_split(name)
I'm stuck on how to access anything from the resulting tibbles without repeatedly using indexing formatting. I've been able to pull tcontents of individual tibbles by using the following code, but I know there has to be a faster and cleaner way to do this!
playtbls[[1]][["content"]]
My ultimate goal is to be able to append the contents of these tibbles to a new dataframe "plays" which contains the title and genre of each play, where each play is an individual row. Basically, I need to be able to say for each play's name, when plays$name == playtbls[[x]][["name"]], append playtbls[[x]][["content"]] to the plays dataframe
The data is available from the bardr package.
We can use
lapply(playtbls, `[[`, "content")
Or with tidyverse
library(purrr)
library(dplyr)
map(playtbls, ~ .x %>%
pluck(content))

How to modify a dataframe that can't be called directly?

I'm creating a function to that several data frames automatically. How can I call those data.frames to mutate them?
For example, say I created a data for which each item is meant to become a dataframe like so:
assign(paste0("d","f"),c(tree,fox,river))
Then I take an item from the list and use it to name a dataframe.
assign(paste(get(paste0("d","f"))[1]),as.data.frame(c(1,2,3))
so that now if i do:
get(paste(get(paste0("d","f"))[1]))
it returns a data frame with 1,2,3
Here's my problem, I want to be able to modify those items so something like
get(paste(get(paste0("d","f"))[1]))[1] <- 4
#So that now if i do
get(paste(get(paste0("d","f"))[1]))
it returns a data frame with 4,2,3
It is better not to create multiple objects in the global environment. If it is already created, load them into a list and do all the changes/transformations/mutates etc. in the list. It would make easier to read/write in list rather than looking for these objects floating in the global env
lapply(mget(paste0("df", 1:3)), function(x) {x[[1]] <- 4; x})

How to merge a set of lists into a single data frame

I am new to R and coding in general, so please bear with me.
I have a spreadsheet that has 7 sheets, 6 of these sheets are formatted in the same way and I am skipping the one that is not formatted the same way.
The code I have is thus:
lst <- lapply(2:7,
function(i) read_excel("CONFIDENTIAL Ratio 062018.xlsx", sheet = i)
)
This code was taken from this post: How to import multiple xlsx sheets in R
So far so good, the formula works and I have a large list with 6 sub lists that appears to represent all of my data.
It is at this point that I get stuck, being so new I do not understand lists yet, and really need the lists to be merged into one single data frame that looks and feels like the source data (so columns and rows).
I cannot work out how to get from a list to a single data frame, I've tried using R Bind and other suggestions from here, but all seem to either fail or only partially work and I end up with a data frame that looks like a list etc.
If each sheets has the same number of columns (ncol) and same names (colnames) then this will work. It needs the dplyr pacakge.
require(dplyr)
my_dataframe <- bind_rows(my_list)

Changing a List to a Dataframe in R

I have used the "htmltab" library to get data on the NFL draft and combine. The data has been selected fine but they are lists at the moment. I intend to merge them and perform analysis the data. at the moment it looks like this:
image List of combine 2016 1
Whenever I try use the unlist method I lose the headers of the columns and they are still remaining as a list.
any suggestions on this?
urlcom16 <- "http://nflcombineresults.com/nflcombinedata.php?
year=2016&pos=&college="
com16 <- htmltab(doc=urlcom16, which=1)
Try as.data.frame(com16). If it doesn't work, you might not have the same vector length in each list entry.

r create and address variable in for loop

I have multiple csv-files in one folder. I want to load each csv-file in this folder into one separate data frame. Next, I want to extract certain elements from this data frame into a matrix and calculate the mean of all these matrixes.
setwd("D:\\data")
group_1<-list.files()
a<-length(group_1)
mferg_mean<-data.frame
for(i in 1:a)
{
assign(paste0("mferg_",i),read.csv(group_1[i],header=FALSE,sep=";",quote="",dec=",",col.names=1:90))
}
As there are 11 csv-files in the folder I now have the data frames
mferg_1
to
mferg_11
How can I address each data frame in this loop? As mentioned, I want to extract certain elements from each data frame to a matrix. I would imagine it something like this:
assign(paste0("mferg_matrix_",i),mferg_i[1:5,1:10])
But this obviously does not work because R does not recognize mferg_i in the loop. How can I address this data frame?
This is not something you should probably be using assign for in the first place. Working with a bunch of different data.frames in R is a mess, but working with a list of data.frames is much easier. Try reading your data with
group_1<-list.files()
mferg <- lapply(group_1, function(filename) {
read.csv(filename,header=FALSE,sep=";",quote="",dec=",",col.names=1:90))
})
and you get each each value with mferg[[1]], mferg[[1]], etc. And then you can create a list of extractions with
mferg_matrix <- lapply(mferg, function(x) x[1:5, 1:10])
This is the more R-like way to do things.
But technically you can use get to retrieve values like you use assign to create them. For example
assign(paste0("mferg_matrix_",i),get(paste0("mferg_",i))[1:5,1:10])
but again, this is probably not a smart strategy in the long run.

Resources