英文:
Subset data frame in R and name each subset in the loop
问题
我正在尝试使用列中的一组唯一值对数据框进行子集操作,并根据该唯一值为每个子集命名,在R中。
我已成功地对数据框进行了子集操作,但我不确定如何根据它们进行子集操作的值来命名每个子集。我已经链接了一个测试数据集和我的子集数据的代码。
我只需弄清楚如何在这个过程中命名每个子集。
**更新
我找到了一个类似的问题,它是根据列名来重命名子集的,但它是在处理年份序列而不是非连续ID时的。我不太确定如何调整它。
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
测试数据
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"))
英文:
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.
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"))
答案1
得分: 0
以下是您要翻译的内容:
似乎@benson 步入正轨。
我首先使用了如下的拆分操作
subset_data <- split(test, test$ID)
然后运行了以下循环来重命名所有内容
for (i in seq_along(subset_data)) {
assign(paste0(names(subset_data)[i]), subset_data[[i]])}
英文:
It seems that @benson was on the right track.
I first used split as shown
subset_data <- split(test, test$ID)
Then ran the following loop to rename everything
for (i in seq_along(subset_data)) {
assign(paste0(names(subset_data)[i]), subset_data[[i]])}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论