'How to change the names of rows in a column specifically

I want to change the names in my data frame from "Keratalla cochlearis" to "Keratalla" in a column. The data roughly looks like this:

...1 sampleID                             B     type  length_pixel date       lakeID split well  depth length_mm biomass_mgAFDM  year BACI  repID
    <dbl> <chr>                                <chr> <chr>        <dbl> <date>     <chr>  <dbl> <chr> <chr>     <dbl>          <dbl> <int> <chr> <chr>
 1 175591 WE2 Ö5 2020-08-20 0-6m_Split2_Well1~ 144a~ Keratalla cochlearis 14.0 2020-08-12 WE2        2 Well~ 0-6m     0.0589    0.000000615  2020 Refe~ WE2_~
 2 152468 WE2 Ö0 2020-05-27 2.5m_Split1_Well2~ a948~ Keratalla cochlearis 17.5 2020-05-27 WE2        1 Well~ 2.5m     0.0735    0.000000793  2020 Refe~ WE2_~
 3 175542 WE2 Ö4 2020_Split2_Well4.png         1e0e~ Keratalla         20.0 2020-07-27 WE2        2 Well~ 0-6m     0.0841    0.00000119   2020 Refe~ WE2_~
 4 156248 WE2 Ö1 2020-05-27_Split1_Well3.png   a54f~ Keratalla         20.2 2020-05-27 WE2        1 Well~ 0-6m     0.0850    0.00000123   2020 Refe~ WE2_~
 5 127090 WE2 2018-09-28_Split1_Well3.png      0ae8~ Keratalla         17.7 2018-09-28 WE2        1 Well~ 0-6m     0.0744    0.00000124   2018 Refe~ WE2_~
 6  24324 WE1 2018-08-02_Split1_Well1-1.png    ce60~ Keratalla         17.8 2018-08-02 WE1        1 Well~ 0-6m     0.0748    0.00000125   2018 Cont~ WE1_~
 7 166850 WE2 Ö3 2020 6-0m_Split2_Well3.png    ba50~ Keratalla         20.6 2020-07-05 WE2        2 Well~ 0-6m     0.0866    0.00000130   2020 Refe~ WE2_~
 8 179771 WE2 Ö6 2020_Split1_Well1.png         c440~ Keratalla         20.6 2020-09-07 WE2        1 Well~ 0-6m     0.0866    0.00000130   2020 Refe~ WE2_~
 9 166694 WE2 Ö3 2020 6-0m_Split2_Well3.png    59a7~ Keratalla         21.1 2020-07-05 WE2        2 Well~ 0-6m     0.0886    0.00000139   2020 Refe~ WE2_~
10 166849 WE2 Ö3 2020 6-0m_Split2_Well3.png    54c7~ Keratalla         21.1 2020-07-05 WE2        2 Well~ 0-6m     0.0886    0.00000139   2020 Refe~ WE2_~

Dput of the 10 first rows:

structure(list(lakeID = c("WE1", "WE1", "WE1", "WE1", "WE1", 
"WE1", "WE1", "WE1", "WE1", "WE1"), date = structure(c(17309, 
17309, 17309, 17309, 17309, 17309, 17309, 17315, 17315, 17315
), class = "Date"), depth = c("0-6m", "0-6m", "0-6m", "0-6m", 
"0-6m", "0-6m", "0-6m", "0-6m", "0-6m", "0-6m"), type = c("Bosmina", 
"Calanoid", "Keratalla cochlearis", "Keratalla cochlearis", "Kellicottia", "Keratella", 
"Nauplii", "Asplanchna", "Keratalla cochlearis", "Calanoid"), repID = c("WE1_0-6m_2017-05-23_Control", 
"WE1_0-6m_2017-05-23_Control", "WE1_0-6m_2017-05-23_Control", 
"WE1_0-6m_2017-05-23_Control", "WE1_0-6m_2017-05-23_Control", 
"WE1_0-6m_2017-05-23_Control", "WE1_0-6m_2017-05-23_Control", 
"WE1_0-6m_2017-05-29_Control", "WE1_0-6m_2017-05-29_Control", 
"WE1_0-6m_2017-05-29_Control"), BACI = c("Control", "Control", 
"Control", "Control", "Control", "Control", "Control", "Control", 
"Control", "Control"), Zoocount = c(95L, 50L, 488L, 33L, 635L, 
12L, 597L, 1L, 57L, 57L), year = c(2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017, 2017, 2017)), row.names = c(NA, -10L), class = "data.frame")

I've tried Zoos$type[Zoos$type == "Keratalla cochlearis"] <- "Keratalla" to no success.



Solution 1:[1]

You can use the following code:

library(dplyr)
Zoos %>% 
  mutate(type = ifelse(as.character(type) == "Keratalla cochlearis", "Keratalla", as.character(type)))

Output:

   lakeID       date depth        type                       repID    BACI Zoocount year         typ
1     WE1 2017-05-23  0-6m     Bosmina WE1_0-6m_2017-05-23_Control Control       95 2017     Bosmina
2     WE1 2017-05-23  0-6m    Calanoid WE1_0-6m_2017-05-23_Control Control       50 2017    Calanoid
3     WE1 2017-05-23  0-6m   Keratalla WE1_0-6m_2017-05-23_Control Control      488 2017   Keratalla
4     WE1 2017-05-23  0-6m   Keratalla WE1_0-6m_2017-05-23_Control Control       33 2017   Keratalla
5     WE1 2017-05-23  0-6m Kellicottia WE1_0-6m_2017-05-23_Control Control      635 2017 Kellicottia
6     WE1 2017-05-23  0-6m   Keratella WE1_0-6m_2017-05-23_Control Control       12 2017   Keratella
7     WE1 2017-05-23  0-6m     Nauplii WE1_0-6m_2017-05-23_Control Control      597 2017     Nauplii
8     WE1 2017-05-29  0-6m  Asplanchna WE1_0-6m_2017-05-29_Control Control        1 2017  Asplanchna
9     WE1 2017-05-29  0-6m   Keratalla WE1_0-6m_2017-05-29_Control Control       57 2017   Keratalla
10    WE1 2017-05-29  0-6m    Calanoid WE1_0-6m_2017-05-29_Control Control       57 2017    Calanoid

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1