garich
garich

Reputation: 31

R: how do I programmatically loop through a split data frame

I have data set where data for a series of lots is stored sequentially down a column, and multiple parameters are given for each lot (also sequentially). The file looks something like this:

LotID,Param,Nominal,Value
R0001,Len,1.2500,1.234
R0001,Dia,2.0000,1.979
R0002,Len,1.2500,1.252
R0002,Dia,2.0000,2.010

I'm able to drill down to the data I need by importing it into a data frame, splitting the data frame by LotID, and then splitting again by Param, which is great. Here is the code I am using for that:

myCapFull <- read.csv("capabilityFull.csv")
myCapSplit <- split(myCapFull, myCapFull$LotID)
myR0001 <- split(myCapSplit$R0001,myCapSplit$R0001$Param)
myR0001$Dia$Value # Returns 1.979

But what I want to do is use iter to iterate over each parameter of each lot, and I can't find a way to do that programmatically. I know how to write the code if I know all of the names in the LotID field, but that doesn't help inside a for/next loop. I have a feeling that I'm just missing one very simple command, and I've spent a lot of time searching but haven't found the answer. I'm new to R, this is really my first real-world application of it, so any help would be much appreciated.

Upvotes: 2

Views: 3758

Answers (2)

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

If you don't know the values in LotID, you can access the data frames in your list with numeric indices:

> myCapSplit[[1]]
  LotID Param Nominal Value
1 R0001   Len    1.25 1.234
2 R0001   Dia    2.00 1.979
> 
> myCapSplit[[2]]
  LotID Param Nominal Value
3 R0002   Len    1.25 1.252
4 R0002   Dia    2.00 2.010

Upvotes: 1

BrodieG
BrodieG

Reputation: 52637

Maybe you're looking for subset?

subset(myCapFull, Param=="Dia" & LotID == "R0001")
#   LotID Param Nominal Value
# 2 R0001   Dia       2 1.979    

Alternatively, you can look into documentation for [.data.frame for more info on how to subset, or into data.table, dplyr, or plyr packages for manipulation of data frames by groups (i.e. split apply combine analysis). For example, here we find the mean of each parameter across all lots with data.table:

library(data.table)
DT <- data.table(myCapFull)
DT[, mean(Value), by=Param]
#    Param     V1
# 1:   Len 1.2430
# 2:   Dia 1.9945

Upvotes: 0

Related Questions