Home > database >  R: changing a variable to another value in some rows
R: changing a variable to another value in some rows

Time:10-31

I am an R newbie and I would like to change a value within a column of specific rows (pertaining to site names). Specifically, at some sites, I would like to change the value of my 'country' variable to "Indonesia", they are currently all "French Polynesia" because I found it easier to do that... but any suggestions are welcome. I tried a couple things in the code below:

#now add country to the correct sites: 2019
fish_2019_forplot$country <- c("French Polynesia")

if(fish_2019_forplot$reef_name %in% c['Coral Beach', 'Sental Restoration Site',
        'Toya Pakeh', 'Biorock', 'Halik', 'Lembongan Bay', 'House Reef SORCE',
        'Biorock TDC', 'Gili Goleng', 'Sental (Pre-Restoration Site)',
        'SD Reef (east of blocks)', 'PMG Pertamina Bommie',
        'Manta Point', 'Sunken Island', 'Sental Reef (Reference site) - Nusa Penida',
        'Crystal Bay', 'SD Reef, Mangrove'], "Indonesia")

fish_2019_forplot %>% 
  mutate(country = "Indonesia" if ) 

(see code) I tried many versions of what I show here.

CodePudding user response:

For a better chance of getting an answer - please try to include a sample of your data.

Try this

library(tidyverse)

indo_reef <- c('Coral Beach', 'Sental Restoration Site',
  'Toya Pakeh', 'Biorock', 'Halik', 'Lembongan Bay', 'House Reef SORCE',
  'Biorock TDC', 'Gili Goleng', 'Sental (Pre-Restoration Site)',
  'SD Reef (east of blocks)', 'PMG Pertamina Bommie',
  'Manta Point', 'Sunken Island', 'Sental Reef (Reference site) - Nusa Penida',
  'Crystal Bay', 'SD Reef, Mangrove')

fish_2019_forplot %>%  
  mutate(country = case_when(reef_name %in% indo_reef ~ "Indonesia", 
                             TRUE ~ country))
  • Related