[英]Replace the values of one column with values of another column in R dplyr
I have a data frame that looks like this.我有一个看起来像这样的数据框。 I want inside the dplyr pipeline to replace only the 7 first rows of the threshold column with the values that come from the manufacturer column.
我想在 dplyr 管道内仅用来自制造商列的值替换阈值列的前 7 行。
library(tidyverse)
mpg %>%
arrange(cty) %>%
mutate(threshold=NA) %>%
select(manufacturer,cty, threshold)
#> # A tibble: 234 × 3
#> manufacturer cty threshold
#> <chr> <int> <lgl>
#> 1 dodge 9 NA
#> 2 dodge 9 NA
#> 3 dodge 9 NA
#> 4 dodge 9 NA
#> 5 jeep 9 NA
#> 6 chevrolet 11 NA
#> 7 chevrolet 11 NA
#> 8 chevrolet 11 NA
#> 9 dodge 11 NA
#> 10 dodge 11 NA
#> # … with 224 more rows
Created on 2022-08-31 with reprex v2.0.2使用reprex v2.0.2创建于 2022-08-31
I want my data to look like this我希望我的数据看起来像这样
#> manufacturer cty threshold
#> <chr> <int> <lgl>
#> 1 dodge 9 dodge
#> 2 dodge 9 dodge
#> 3 dodge 9 dodge
#> 4 dodge 9 dodge
#> 5 jeep 9 jeep
#> 6 chevrolet 11 chevrolet
#> 7 chevrolet 11 chevrolet
#> 8 chevrolet 11 NA
#> 9 dodge 11 NA
#> 10 dodge 11 NA
any help and guidance as always are highly appreciated任何帮助和指导一如既往地受到高度赞赏
USe case_when
to create a logical condition with row_number()
for replacement.使用
case_when
创建带有row_number()
的逻辑条件以进行替换。 In addition, there is no need to create a blank column ie the NAs
can be filled by default in case_when
另外,不需要创建一个空白列,即在
NAs
中可以默认填写case_when
library(dplyr)
library(ggplot2)
mpg %>%
arrange(cty) %>%
select(manufacturer, cty) %>%
mutate(threshold = case_when(row_number() < 7 ~manufacturer))
-output -输出
# A tibble: 234 × 3
manufacturer cty threshold
<chr> <int> <chr>
1 dodge 9 dodge
2 dodge 9 dodge
3 dodge 9 dodge
4 dodge 9 dodge
5 jeep 9 jeep
6 chevrolet 11 chevrolet
7 chevrolet 11 <NA>
8 chevrolet 11 <NA>
9 dodge 11 <NA>
10 dodge 11 <NA>
# … with 224 more rows
Here is version using row_number()
with ifelse
and %in%
:这是使用带有
ifelse
和%in%
row_number()
的版本:
mpg %>%
arrange(cty) %>%
mutate(threshold=NA) %>%
select(manufacturer,cty, threshold) %>%
mutate(threshold = ifelse(row_number() %in% 1:7, manufacturer, threshold))
manufacturer cty threshold
<chr> <int> <chr>
1 dodge 9 dodge
2 dodge 9 dodge
3 dodge 9 dodge
4 dodge 9 dodge
5 jeep 9 jeep
6 chevrolet 11 chevrolet
7 chevrolet 11 chevrolet
8 chevrolet 11 NA
9 dodge 11 NA
10 dodge 11 NA
# ... with 224 more rows
# i Use `print(n = ...)` to see more rows
Here's yet another version.这是另一个版本。 Similar to the ones above, but using the function
rowid_to_column()
.与上述类似,但使用 function
rowid_to_column()
。
library(dplyr)
mpg %>%
arrange(cty) %>%
select(manufacturer, cty) %>%
rowid_to_column() %>%
mutate(threshold = ifelse(rowid < 8, manufacturer, NA))
# A tibble: 234 x 4
rowid manufacturer cty threshold
<int> <chr> <int> <chr>
1 1 dodge 9 dodge
2 2 dodge 9 dodge
3 3 dodge 9 dodge
4 4 dodge 9 dodge
5 5 jeep 9 jeep
6 6 chevrolet 11 chevrolet
7 7 chevrolet 11 chevrolet
8 8 chevrolet 11 NA
9 9 dodge 11 NA
10 10 dodge 11 NA
# ... with 224 more rows
# i Use `print(n = ...)` to see more rows
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.