英文:
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 <- 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
And I would like to replace the the values in row 2 with some mapping:
apple -> 1
banana -> 2
So that the resulting output is:
question a b c
> <int> <chr> <chr> <chr>
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 |>
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"))
答案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 %>%
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
<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] <- ifelse(df[2, 2:4] == "apple", 1,
ifelse(df[2, 2:4] == "banana", 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, ] <- c(1, 2, df[2, ])[match(df[2, ], c("apple", "banana", df[2, ]))]
# question a b c
#1 1 chicken chicken beef
#2 2 1 2 1
#3 3 beer beer wine
也可以更容易地在一个函数中使用:
replace_row <- function(data, row, new, old){
data[row, ] <- c(new, data[row, ])[match(data[row, ], c(old, data[row, ])]
data
}
replace_row(df, 2, c(1, 2), c("apple", "banana"))
# question a b c
#1 1 chicken chicken beef
#2 2 1 2 1
#3 3 beer beer wine
<details>
<summary>英文:</summary>
Here's a way with `match`:
```r
df[2, ] <- c(1, 2, df[2, ])[match(df[2, ], c("apple", "banana", 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 <- function(data, row, new, old){
data[row, ] <- c(new, data[row, ])[match(data[row, ], c(old, data[row, ]))]
data
}
replace_row(df, 2, c(1, 2), c("apple", "banana"))
# question a b c
#1 1 chicken chicken beef
#2 2 1 2 1
#3 3 beer beer wine
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论