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

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

Map values in dataframe row to new values

问题

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

  1. df <- tibble(question = 1:3, a = c('chicken', 'apple', 'beer'), b = c('chicken', 'banana', 'beer'), c = c('beef', 'apple', 'wine'))
  2. question a b c
  3. > <int> <chr> <chr> <chr>
  4. 1 1 chicken chicken beef
  5. 2 2 apple banana apple
  6. 3 3 beer beer wine

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

  1. apple -> 1
  2. banana -> 2

这样得到的结果是:

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

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

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

英文:

I have a dataframe, say:

  1. 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;))
  2. question a b c
  3. &gt; &lt;int&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  4. 1 1 chicken chicken beef
  5. 2 2 apple banana apple
  6. 3 3 beer beer wine

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

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

So that the resulting output is:

  1. question a b c
  2. &gt; &lt;int&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  3. 1 1 chicken chicken beef
  4. 2 2 1 2 1
  5. 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

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

  1. "If the row isn't 2, leave the values in a:c alone, otherwise swap in these new values":
  2. df |>
  3. mutate(across(a:c, ~if_else(row_number() != 2, .,
  4. case_match(., "apple" ~ "1", "banana" ~ "2")))
  5. Result
  6. question a b c
  7. 1 1 chicken chicken beef
  8. 2 2 1 2 1
  9. 3 3 beer beer wine
  10. -----
  11. df <- data.frame(question = 1:3,
  12. a = c("chicken", "apple", "beer"),
  13. b = c("chicken", "banana", "beer"),
  14. c = c("beef", "apple", "wine"))

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

英文:

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

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

Result

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

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

答案2

得分: 4

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

  1. library(dplyr)
  2. df %>%
  3. mutate(across(a:c, ~ case_when(row_number() == 2 & . == "apple" ~ '1',
  4. row_number() == 2 & . == "banana" ~ '2',
  5. TRUE ~ .)))
  6. #> question a b c
  7. #> 1 1 chicken chicken beef
  8. #> 2 2 1 2 1
  9. #> 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:

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

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

答案3

得分: 2

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

  1. df[2, 2:4] <- ifelse(df[2, 2:4] == "apple", 1,
  2. 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:

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

答案4

得分: 2

  1. 使用命名向量

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

  1. -输出

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

  1. <details>
  2. <summary>英文:</summary>
  3. Using a named vector

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

  1. -output

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

  1. </details>
  2. # 答案5
  3. **得分**: 1
  4. 以下是已翻译的代码部分:
  5. ```r
  6. 这是一种使用 `match` 的方法:
  7. ```r
  8. df[2, ] &lt;- c(1, 2, df[2, ])[match(df[2, ], c(&quot;apple&quot;, &quot;banana&quot;, df[2, ]))]
  9. # question a b c
  10. #1 1 chicken chicken beef
  11. #2 2 1 2 1
  12. #3 3 beer beer wine

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

  1. replace_row &lt;- function(data, row, new, old){
  2. data[row, ] &lt;- c(new, data[row, ])[match(data[row, ], c(old, data[row, ])]
  3. data
  4. }
  5. replace_row(df, 2, c(1, 2), c(&quot;apple&quot;, &quot;banana&quot;))
  6. # question a b c
  7. #1 1 chicken chicken beef
  8. #2 2 1 2 1
  9. #3 3 beer beer wine
  1. <details>
  2. <summary>英文:</summary>
  3. Here&#39;s a way with `match`:
  4. ```r
  5. df[2, ] &lt;- c(1, 2, df[2, ])[match(df[2, ], c(&quot;apple&quot;, &quot;banana&quot;, df[2, ]))]
  6. # question a b c
  7. #1 1 chicken chicken beef
  8. #2 2 1 2 1
  9. #3 3 beer beer wine

Might be easier to use in a function:

  1. replace_row &lt;- function(data, row, new, old){
  2. data[row, ] &lt;- c(new, data[row, ])[match(data[row, ], c(old, data[row, ]))]
  3. data
  4. }
  5. replace_row(df, 2, c(1, 2), c(&quot;apple&quot;, &quot;banana&quot;))
  6. # question a b c
  7. #1 1 chicken chicken beef
  8. #2 2 1 2 1
  9. #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:

确定