Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

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

I want my data to look like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#>    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

>Solution :

USe case_when to create a logical condition with row_number() for replacement. In addition, there is no need to create a blank column i.e. the NAs can be filled by default in 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading