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

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

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

问题

Here is the translated code part you requested:

我不是一个编程专家,但我正在尝试学习R,同时处理我的数据。我想要创建一个循环,以便遍历每一列,并输出这些列中最高值的行名称(按从最高值到最低值的顺序)。我特别想要创建一个循环,而不是手动逐列处理,因为我的数据集有很多列,我真的想学会使用循环。这是我的示例数据:

```R
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")

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

表格示例输出:

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

"列表"示例输出:

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

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

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

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

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

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

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

帮助?


Please note that the code is provided as-is, and it may require further adjustments to work correctly in your R environment.

<details>
<summary>英文:</summary>

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")


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. 

example output as a table:

| value_1  | value_2  | value_3  |
| -------- | -------- | -------- |
| first    | first    | first    |
| second   | second   | second   |
| third    | third    | third    |
| fourth   | fourth   | fourth   |


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"



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)
}


this resulted in the following error message
   Error in UseMethod(&quot;arrange&quot;) : 
     no applicable method for &#39;arrange&#39; applied to an object of class &quot;c(&#39;integer&#39;, &#39;numeric&#39;)&quot;

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. 

Help?

</details>


# 答案1
**得分**: 1

以下是翻译好的部分:

"While this can be done without a loop, it sounds like you'd like to fix your current loop solution." => "虽然这可以在没有循环的情况下完成,但听起来你想修复你当前的循环解决方案。"

"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`。所以应该添加它。"

"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`中的列。"

"This example stores the result in a list." => "这个示例将结果存储在一个列表中。"

"$value_1" => "$value_1"

"[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;"

"$value_2" => "$value_2"

"[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;"

"$value_3" => "$value_3"

"[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;"

<details>
<summary>英文:</summary>

While this can be done without a loop, it sounds like you&#39;d like to fix your current loop solution.

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.

Second, `i` is a character value, not a symbol. So you can use the `.data` pronoun with `i` to access the column within `mydata`.

This example stores the result in a list.

    lst &lt;- list()
    
    for (i in colnames(mydata)) {
      lst[[i]] &lt;- mydata %&gt;%
        arrange(desc(.data[[i]])) %&gt;% 
        rownames_to_column() %&gt;% 
        top_n(4, i) %&gt;% 
        pull(rowname)
    }

**Output**

    $value_1
    [1] &quot;first&quot;  &quot;second&quot; &quot;third&quot;  &quot;fourth&quot; &quot;fifth&quot; 
    
    $value_2
    [1] &quot;first&quot;  &quot;second&quot; &quot;third&quot;  &quot;fourth&quot; &quot;fifth&quot; 
    
    $value_3
    [1] &quot;first&quot;  &quot;second&quot; &quot;third&quot;  &quot;fourth&quot; &quot;fifth&quot; 

</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:

确定