I'm trying to subset a data frame using a list of unique values in a column and name each subset based on that unique value in R.
I've been able to successfully subset the data frame, but I'm not sure how to name each subset based on the values they were subset with. I've linked a test data set and my code that subsets the data.
I just need to figure out how to name each subset as part of the process.
**UPDATE
I found a similar question that looked to rename subsets based on column names, but it was working through a sequence of years instead of non-sequential IDs. I'm not quite sure how to adapt it.
How to write a loop to subset data and rename the subsample
List <- unique(test$ID)
for (i in 1:length(List)) {
assign(paste0("test",i), subset(test, ID == List[[i]]))
}
Column | ID | Value
1 | a | 5
2 | a | 6
3 | b | 4
4 | b | 1
5 | c | 9
6 | c | 5
7 | c | 7
8 | d | 1
9 | e | 1
10 | d | 5
11 | d | 6
12 | f | 7
13 | g | 8
14 | g | 9
15 | g | 1
16 | g | 12
17 | h | 6
Test Data
structure(list(ID = c("a", "a", "b", "b", "c", "c", "c", "d",
"e", "d", "d", "f", "g", "g", "g", "g", "h"), Value = c(5, 6,
4, 1, 9, 5, 7, 1, 1, 5, 6, 7, 8, 9, 1, 12, 6)), row.names = c(NA,
-17L), spec = structure(list(cols = list(ID = structure(list(), class = c("collector_character",
"collector")), Value = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x000001b7992ef930>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))