I am using R and I have the following data frame:
data <- data.frame(ID_CODE = c('001', '001', '001', '002', '002', '003'),
Metric1 = c('0.94', '0.68', '0.8', '0.12', '0.56', '0.87'))
I would like to create a loop that: (1) applies a subset to the data frame and (2) creates for each unique identifier in ID_CODE a separate data frame that includes all the rows pertaining to that identifier.
As a first step, I used pull to get all identifiers into a list:
# Get the identifiers
Identifiers <- dplyr::pull(data, ID_CODE)
Then, I tried to create the loop: I managed to subset, but when I tried to store each subset in a different dataframe named "temp_[Identifier]", it doesn't work (object 'temp_' not found)
for (i in unique(data$ID_CODE)) {
temp_[[i]] <- subset(data, ID_CODE == i)
}
The resulting data frames should be:
temp_001 <- data.frame(ID_CODE = c('001', '001', '001'),
Metric1 = c('0.94', '0.68', '0.8'))
temp_002 <- data.frame(ID_CODE = c('002', '002'),
Metric1 = c('0.12', '0.56'))
temp_003 <- data.frame(ID_CODE = c('003'),
Metric1 = c('0.87'))
Can anybody help?