在R中对数据框进行子集操作,并在循环中为每个子集命名。

huangapple go评论74阅读模式
英文:

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.

https://stackoverflow.com/questions/49639338/how-to-write-a-loop-to-subset-data-and-rename-the-subsample

List &lt;- unique(test$ID)

for (i in 1:length(List)) {
  assign(paste0(&quot;test&quot;,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(&quot;a&quot;, &quot;a&quot;, &quot;b&quot;, &quot;b&quot;, &quot;c&quot;, &quot;c&quot;, &quot;c&quot;, &quot;d&quot;, 
&quot;e&quot;, &quot;d&quot;, &quot;d&quot;, &quot;f&quot;, &quot;g&quot;, &quot;g&quot;, &quot;g&quot;, &quot;g&quot;, &quot;h&quot;), 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(&quot;collector_character&quot;, 
&quot;collector&quot;)), Value = structure(list(), class = c(&quot;collector_double&quot;, 
&quot;collector&quot;))), default = structure(list(), class = c(&quot;collector_guess&quot;, 
&quot;collector&quot;)), delim = &quot;,&quot;), class = &quot;col_spec&quot;), problems = &lt;pointer: 0x000001b7992ef930&gt;, class = c(&quot;spec_tbl_df&quot;, 
&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))

答案1

得分: 0

以下是您要翻译的内容:

似乎@benson 步入正轨。
我首先使用了如下的拆分操作

subset_data &lt;- 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 &lt;- 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]])}

huangapple
  • 本文由 发表于 2023年5月26日 13:57:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定