将数据框行中的数值映射到新数值。

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

Map values in dataframe row to new values

问题

我有一个数据框,如下所示:

df <- tibble(question = 1:3, a = c('chicken', 'apple', 'beer'), b = c('chicken', 'banana', 'beer'), c = c('beef', 'apple', 'wine'))

question a       b       c    
>      <int> <chr>   <chr>   <chr>
1        1 chicken chicken beef 
2        2 apple   banana  apple
3        3 beer    beer    wine 

我想要用一些映射来替换第2行的值:

apple -> 1
banana -> 2

这样得到的结果是:

question a       b       c    
>      <int> <chr>   <chr>   <chr>
1        1 chicken chicken beef 
2        2       1       2        1
3        3 beer    beer    wine 

我已经查看了case_match,但它似乎需要一个向量,而我的数据是按行而不是按列。我认为我可以使用across()来将这个操作应用到特定行中的所有列,但不确定如何将这些部分组合在一起。

编辑:我不感兴趣替换特定列和行的特定索引处的值。我对通过特定映射重新编码一行中的所有值感兴趣。

英文:

I have a dataframe, say:

df &lt;- tibble(question = 1:3, a = c(&#39;chicken&#39;, &#39;apple&#39;, &#39;beer&#39;), b = c(&#39;chicken&#39;, &#39;banana&#39;, &#39;beer&#39;), c = c(&#39;beef&#39;, &#39;apple&#39;, &#39;wine&#39;))

question a       b       c    
&gt;      &lt;int&gt; &lt;chr&gt;   &lt;chr&gt;   &lt;chr&gt;
1        1 chicken chicken beef 
2        2 apple   banana  apple
3        3 beer    beer    wine 

And I would like to replace the the values in row 2 with some mapping:

apple -&gt; 1
banana -&gt; 2

So that the resulting output is:

question a       b       c    
&gt;      &lt;int&gt; &lt;chr&gt;   &lt;chr&gt;   &lt;chr&gt;
1        1 chicken chicken beef 
2        2       1       2        1
3        3 beer    beer    wine 

I've looked at case_match, but it appears to want a vector and my data is rows rather than columns. I think I can use across() to just get this to apply to all columns in a particular row, but not sure how to fit these pieces together.

EDIT: I'm not interested in replacing the value at a specific index of column and row. I'm interested in recoding all the values in a row by a particular mapping.

答案1

得分: 5

以下是您要的代码部分的翻译:

"If the row isn't 2, leave the values in a:c alone, otherwise swap in these new values":

    df |>
      mutate(across(a:c, ~if_else(row_number() != 2, .,
                            case_match(., "apple" ~ "1", "banana" ~ "2")))

Result

      question       a       b    c
    1        1 chicken chicken beef
    2        2       1       2    1
    3        3    beer    beer wine

-----

df <- data.frame(question = 1:3,
               a = c("chicken", "apple", "beer"),
               b = c("chicken", "banana", "beer"),
               c = c("beef", "apple", "wine"))

希望这些翻译对您有所帮助。

英文:

"If the row isn't 2, leave the values in a:c alone, otherwise swap in these new values":

df |&gt;
  mutate(across(a:c, ~if_else(row_number() != 2, .,
                        case_match(., &quot;apple&quot; ~ &quot;1&quot;, &quot;banana&quot; ~ &quot;2&quot;))))

Result

  question       a       b    c
1        1 chicken chicken beef
2        2       1       2    1
3        3    beer    beer wine

df &lt;- data.frame(question = 1:3,
           a = c(&quot;chicken&quot;, &quot;apple&quot;, &quot;beer&quot;),
           b = c(&quot;chicken&quot;, &quot;banana&quot;, &quot;beer&quot;),
           c = c(&quot;beef&quot;, &quot;apple&quot;, &quot;wine&quot;))

答案2

得分: 4

您可以使用 across 结合 case_when 并在第二行条件中使用 row_number 来实现只对第二行应用操作,如下所示:

library(dplyr)
df %>%
  mutate(across(a:c, ~ case_when(row_number() == 2 & . == "apple" ~ '1',
                                 row_number() == 2 & . == "banana" ~ '2',
                                 TRUE ~ .)))
#>   question       a       b    c
#> 1        1 chicken chicken beef
#> 2        2       1       2    1
#> 3        3    beer    beer wine

创建于2023年2月8日,使用 reprex v2.0.2

英文:

You could use across with case_when and condition with row_number to apply on only the second row like this:

library(dplyr)
df %&gt;%
  mutate(across(a:c, ~ case_when(row_number() == 2 &amp; . == &quot;apple&quot; ~ &#39;1&#39;,
                                 row_number() == 2 &amp; . == &quot;banana&quot; ~ &#39;2&#39;,
                                 TRUE ~ .)))
#&gt;   question       a       b    c
#&gt; 1        1 chicken chicken beef
#&gt; 2        2       1       2    1
#&gt; 3        3    beer    beer wine

<sup>Created on 2023-02-08 with reprex v2.0.2</sup>

答案3

得分: 2

你可以使用ifelse创建一个条件函数,并在所有列中包括行索引,除了第一列,以应用该函数:

df[2, 2:4] <- ifelse(df[2, 2:4] == "apple", 1,
                    ifelse(df[2, 2:4] == "banana", 2, df[2, 2:4]))
英文:

You can create a conditional function using ifelse and include the row index with all columns but the first one to apply the function:

df[2, 2:4] &lt;- ifelse(df[2, 2:4] == &quot;apple&quot;, 1,
                    ifelse(df[2, 2:4] == &quot;banana&quot;, 2, df[2, 2:4]))

答案4

得分: 2

使用命名向量

df1[2, 2:4] <- setNames(c(1, 2), c("apple", "banana"))[unlist(df1[2,-1])]


-输出

df1
question a b c
1 1 chicken chicken beef
2 2 1 2 1
3 3 beer beer wine


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

Using a named vector

df1[2, 2:4] <- setNames(c(1, 2), c("apple", "banana"))[unlist(df1[2,-1])]


-output

> df1
question a b c
1 1 chicken chicken beef
2 2 1 2 1
3 3 beer beer wine


</details>



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

以下是已翻译的代码部分:

```r
这是一种使用 `match` 的方法:
```r
df[2, ] &lt;- c(1, 2, df[2, ])[match(df[2, ], c(&quot;apple&quot;, &quot;banana&quot;, df[2, ]))]

#  question       a       b    c
#1        1 chicken chicken beef
#2        2       1       2    1
#3        3    beer    beer wine

也可以更容易地在一个函数中使用:

replace_row &lt;- function(data, row, new, old){
  data[row, ] &lt;- c(new, data[row, ])[match(data[row, ], c(old, data[row, ])]
  data
}
replace_row(df, 2, c(1, 2), c(&quot;apple&quot;, &quot;banana&quot;))
#  question       a       b    c
#1        1 chicken chicken beef
#2        2       1       2    1
#3        3    beer    beer wine

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

Here&#39;s a way with `match`:
```r
df[2, ] &lt;- c(1, 2, df[2, ])[match(df[2, ], c(&quot;apple&quot;, &quot;banana&quot;, df[2, ]))]

#  question       a       b    c
#1        1 chicken chicken beef
#2        2       1       2    1
#3        3    beer    beer wine

Might be easier to use in a function:

replace_row &lt;- function(data, row, new, old){
  data[row, ] &lt;- c(new, data[row, ])[match(data[row, ], c(old, data[row, ]))]
  data
}
replace_row(df, 2, c(1, 2), c(&quot;apple&quot;, &quot;banana&quot;))
#  question       a       b    c
#1        1 chicken chicken beef
#2        2       1       2    1
#3        3    beer    beer wine

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

发表评论

匿名网友

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

确定