简体   繁体   English

用 R dplyr 中另一列的值替换一列的值

[英]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.

相关问题 将一列中的 NA 替换为 dplyr 中另一列的值 - Replace NAs in one column with the values of another in dplyr 在 r 中使用 dplyr 查找和替换列值 - Lookup and replace column values using dplyr in r 使用 dplyr 有条件地将一列/行中的值替换为另一行/列中的值 - Conditionally replace values in one column/row with values from another row/column using dplyr R在另一列中匹配条件时替换一列中的值 - R replace values in one column upon matching conditions in another column 替换另一列上一列条件中的值:R - Replace values in one column condition on another column: R 在 R 中,当替换列不为空时,如何将一列中的值替换为另一列的值? - In R, how can I replace the values in one column with the values of another column whenever the replacing column is not empty? R dplyr-根据特定值在另一列中的位置从一列中选择值 - R dplyr - select values from one column based on position of a specific value in another column r、dplyr:如何使用 gsub 根据另一列中的值转换一列中的值 - r, dplyr: how to transform values in one column based on value in another column using gsub 根据 R 中的另一列 dataframe 替换一列中的值 - Replace values in one column based on another dataframe in R 尝试在 R 中匹配值并从一列替换到另一列? - Trying to match values and replace from one column to another in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM