Convert Tables to Data Frames with Loop in R

Viewed 41

I have over 100 tables. Each ID has multiple columns (ID, Date, Days, Mass, Float, Date 2, Days 2, pH).
I split the IDs from the data frame and made them the names of the tables as shown below.

data = NN
ID <- paste0("", NN$ID)
SD<- split(NN,ID)
SD

Each of the ID's look as follows

> SD$`4469912`
# A tibble: 5 × 8
       ID Date                 Days  Mass Float `Date 2` `Days 2` pH   
    <dbl> <dttm>              <dbl> <dbl> <dbl> <dttm>      <dbl> <chr>
1 4469912 2022-05-24 00:00:00     0  440   16.9 NA              0 NA   
2 4469912 2022-05-27 00:00:00     3  813   NA   NA              0 NA   
3 4469912 2022-06-02 00:00:00     9  930   NA   NA              0 NA   
4 4469912 2022-06-03 00:00:00    10  914.  NA   NA              0 NA   
5 4469912 2022-06-06 00:00:00    13  944   NA   NA              0 NA 

I would like to convert each ID to its own Dataframe as shown below

`4469912`<- data.frame(SD$`4469912`)

AKA

`4469912`<- data.frame(SD[9])

The problem I am running into is running a loop to create each table as its own data frame. I would like to name the data frames to their corresponding ID. Something along the lines of the code below.

for (x in SD) {
  names(SD[x]) <- data.frame(SD[x])
}

EDIT: I will add that the end goal is to pull or select specific IDs to then plot them on top or against one another in ggplot as each ID is its own geom_line for example:

`4469912`<- data.frame(SD$`4469912`)
`4469822`<- data.frame(SD$`4469822`)
`4469222`<- data.frame(SD$`4469222`)

ggplot(data=NULL,aes(x=`Date`,y=`Mass`)) +
  geom_line(data = `4469912`,aes(col="red"))+
  geom_line(data = `4469822`,aes(col="blue"))+
  geom_line(data = `4469222`,aes(col="green"))

Three data sets are plotted in one graph

Rather than plotting the entirety of my original data frame, I can determine falloff or regression between the IDs rather than the entirety of the data points selected; if that makes sense and/or is relevant.

0 Answers
Related