将循环生成的内容输出到一个列或每个循环一个列表中。

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

Outputting contents generated from loops into one column or list per loop

问题

Here is the translated code part you requested:

  1. 我不是一个编程专家,但我正在尝试学习R,同时处理我的数据。我想要创建一个循环,以便遍历每一列,并输出这些列中最高值的行名称(按从最高值到最低值的顺序)。我特别想要创建一个循环,而不是手动逐列处理,因为我的数据集有很多列,我真的想学会使用循环。这是我的示例数据:
  2. ```R
  3. mydata <- data.frame(
  4. rownames = c("fifth", "second", "third", "first", "fourth"),
  5. value_1 = c(0, 10, 3, 20, 1),
  6. value_2 = c(10, 20, 13, 30, 11),
  7. value_3 = c(5, 15, 8, 25, 6)
  8. ) %>% remove_rownames %>% column_to_rownames(var = "rownames")

对于输出,我希望得到一个表格,其中每一列都有行名称,按每列中最高值到最低值的顺序排列。或者,甚至为每一列创建一个单独的“列表”也可以。

表格示例输出:

value_1 value_2 value_3
first first first
second second second
third third third
fourth fourth fourth

"列表"示例输出:

  1. > value_1
  2. [1] "first" "second" "third" "fourth" "fifth"
  3. > value_2
  4. [1] "first" "second" "third" "fourth" "fifth"
  5. > value_3
  6. [1] "first" "second" "third" "fourth" "fifth"

我的尝试使用top_n(4, i)来返回每列中最高的4个值的行名称,但它没有按值的高低顺序排列行名称。我尝试通过首先使用arrange(desc(i))将行按值从高到低排序来解决这个问题。

  1. for (i in colnames(mydata)){
  2. mydata[, i] <- arrange(mydata, desc(i)) %>% rownames_to_column() %>% top_n(4, i) %>% pull(rowname)
  3. }

这导致了以下错误消息:
Error in UseMethod("arrange") : no applicable method for 'arrange' applied to an object of class "c('integer', 'numeric')"

即使arrange起作用,我也不知道如何将每个循环的结果输出到单独的列或值。

帮助?

  1. Please note that the code is provided as-is, and it may require further adjustments to work correctly in your R environment.
  2. <details>
  3. <summary>英文:</summary>
  4. I am not a coder but I am trying to learn R while I process my data. I want to make a loop to go through each column and output the row names of the highest values in those columns (in order of highest value to lowest value). I specifically want to make a loop instead of doing it manually column-by-column because my dataset has a lot of columns and I really want to learn loops. Here is my example data:

mydata <- data.frame (rownames = c("fifth", "second", "third", "first", "fourth"),
value_1 = c(0,10,3,20,1),
value_2 = c(10,20,13,30,11),
value_3 = c(5,15,8,25,6)
) %>% remove_rownames %>% column_to_rownames(var="rownames")

  1. For the output, I would love a table where each column has the row names in order of highest value to lowest value for each column in mydata. Alternately, even a separate &quot;list&quot; for each column would be fine.
  2. example output as a table:
  3. | value_1 | value_2 | value_3 |
  4. | -------- | -------- | -------- |
  5. | first | first | first |
  6. | second | second | second |
  7. | third | third | third |
  8. | fourth | fourth | fourth |
  9. example output as &quot;lists&quot;:

> value_1
[1] "first" "second" "third" "fourth" "fifth"

> value_2
[1] "first" "second" "third" "fourth" "fifth"

> value_3
[1] "first" "second" "third" "fourth" "fifth"

  1. my attempt at a loop uses top_n(4, i) to return the row names of the highest 4 values in each column, but it doesn&#39;t put the row names in order of how high the value is. I tried to solve this by first sorting the rows by highest to lowest value with arrange(desc(i)).

> for (i in colnames(Sample_Ordering_Table)){
data[,i] <- arrange(desc(i)) %>% rownames_to_column() %>% top_n(4, i) %>% pull(rowname)
}

  1. this resulted in the following error message
  2. Error in UseMethod(&quot;arrange&quot;) :
  3. no applicable method for &#39;arrange&#39; applied to an object of class &quot;c(&#39;integer&#39;, &#39;numeric&#39;)&quot;
  4. Even if the arrange did work, I don&#39;t know how to output the results of each loop to a separate column or value.
  5. Help?
  6. </details>
  7. # 答案1
  8. **得分**: 1
  9. 以下是翻译好的部分:
  10. "While this can be done without a loop, it sounds like you'd like to fix your current loop solution." => "虽然这可以在没有循环的情况下完成,但听起来你想修复你当前的循环解决方案。"
  11. "The first argument of `arrange` is the data.frame, which is missing (often we pipe this in and don't think about it). You provide column names through `i` in your loop, but not `mydata`. So that should be added." => "`arrange`的第一个参数是数据框,这个参数缺失了(通常我们使用管道传递它,不去考虑它)。你通过循环中的`i`提供了列名,但没有提供`mydata`。所以应该添加它。"
  12. "Second, `i` is a character value, not a symbol. So you can use the `.data` pronoun with `i` to access the column within `mydata`." => "其次,`i`是一个字符值,不是一个符号。因此,你可以使用`.data`代词和`i`来访问`mydata`中的列。"
  13. "This example stores the result in a list." => "这个示例将结果存储在一个列表中。"
  14. "$value_1" => "$value_1"
  15. "[1] &quot;first&quot; &quot;second&quot; &quot;third&quot; &quot;fourth&quot; &quot;fifth&quot;" => "[1] &quot;first&quot; &quot;second&quot; &quot;third&quot; &quot;fourth&quot; &quot;fifth&quot;"
  16. "$value_2" => "$value_2"
  17. "[1] &quot;first&quot; &quot;second&quot; &quot;third&quot; &quot;fourth&quot; &quot;fifth&quot;" => "[1] &quot;first&quot; &quot;second&quot; &quot;third&quot; &quot;fourth&quot; &quot;fifth&quot;"
  18. "$value_3" => "$value_3"
  19. "[1] &quot;first&quot; &quot;second&quot; &quot;third&quot; &quot;fourth&quot; &quot;fifth&quot;" => "[1] &quot;first&quot; &quot;second&quot; &quot;third&quot; &quot;fourth&quot; &quot;fifth&quot;"
  20. <details>
  21. <summary>英文:</summary>
  22. While this can be done without a loop, it sounds like you&#39;d like to fix your current loop solution.
  23. The first argument of `arrange` is the data.frame, which is missing (often we pipe this in and don&#39;t think about it). You provide column names through `i` in your loop, but not `mydata`. So that should be added.
  24. Second, `i` is a character value, not a symbol. So you can use the `.data` pronoun with `i` to access the column within `mydata`.
  25. This example stores the result in a list.
  26. lst &lt;- list()
  27. for (i in colnames(mydata)) {
  28. lst[[i]] &lt;- mydata %&gt;%
  29. arrange(desc(.data[[i]])) %&gt;%
  30. rownames_to_column() %&gt;%
  31. top_n(4, i) %&gt;%
  32. pull(rowname)
  33. }
  34. **Output**
  35. $value_1
  36. [1] &quot;first&quot; &quot;second&quot; &quot;third&quot; &quot;fourth&quot; &quot;fifth&quot;
  37. $value_2
  38. [1] &quot;first&quot; &quot;second&quot; &quot;third&quot; &quot;fourth&quot; &quot;fifth&quot;
  39. $value_3
  40. [1] &quot;first&quot; &quot;second&quot; &quot;third&quot; &quot;fourth&quot; &quot;fifth&quot;
  41. </details>

huangapple
  • 本文由 发表于 2023年2月24日 01:54:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548568.html
匿名

发表评论

匿名网友

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

确定