Home > Mobile >  How to replace values of a character column based on the occurrence of specific strings in another c
How to replace values of a character column based on the occurrence of specific strings in another c

Time:12-14

I am struggling with replacing a string at a specific row of my data frame.

In this example, there are mistakes in the class1 column, so I need to change two species classes.

Here is my code:

class1 <- c("Sarcopterygii", "Actinopterygii", "Actinopterygii", 
            "Actinopterygii", "Actinopterygii", "Actinopterygii", 
            "Actinopterygii", "Insecta", "Insecta")

Species1 <-c("Protopterus aethiopicus","Synodontis ruandae",
             "Synodontis afrofischeri", "Hyperolius cinnamomeoventris", 
             "Schilbe intermedius", "Hoplobatrachus occipitalis",
             "Raiamas salmolucius","Trithemis pluvialis","Trithemis donaldsoni")

count1 <- c(12, 2, 32, 4, 6, 7, 8, 13, 10)

Dataset1 <- data.frame(class1, Species1, count1)

I wanted to replace the "Actinopterygii" genus with "Amphibia" for the species "Hyperolius cinnamomeoventris", and also do the same for the species "Hoplobatrachus occipitalis"

CodePudding user response:

It sounds like you're just trying to change two specific values? If so, I would just use base R subsetting:

## Find the species for which the class needs replacing:
rows_to_fix <- Dataset1[["Species1"]] %in% c("Hyperolius cinnamomeoventris", "Hoplobatrachus occipitalis")

## At those elements, replace Class1 with "amphibia"
Dataset1[["Class1"]][rows_to_fix] <- "amphibia"

One advantage of this approach is that it makes it clearer that you're just tweaking a couple of specific errors, rather than generalising for a set of rules

CodePudding user response:

I'm not sure to have understood your goal but here is the main idea with dplyr::case_when

library(dplyr)
Dataset1 %>% 
  mutate(class1 = case_when(class1 == "Actinopterygii" ~ "Hyperolius cinnamomeoventris", TRUE ~ class1))

CodePudding user response:

Try this:

rename Species1

Dataset1[Dataset1$class1 == "Actinopterygii" & Species1 == "Hyperolius cinnamomeoventris", "Species1"] <- "Amphibia"

Dataset1[Dataset1$class1 == "Actinopterygii" & Species1 == "Hoplobatrachus occipitalis", "Species1"] <- "Amphibia"

group dataframe

Dataset2 = Dataset1 %>% group_by(class1, Species1) %>% summarise(count1 = sum(count1))
  • Related