如何根据R数据框的另一列中出现的特定字符串来替换字符列的值?

vu8f3i0k  于 2022-12-20  发布在  其他
关注(0)|答案(3)|浏览(132)

我正在努力替换数据框中特定行的字符串。
在本例中,class1列中有错误,因此我需要更改两个物种类。
下面是我的代码:

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)

对于"Hyperolius cinnamomeoventris"物种,我想用"Amphibia"替换"Actinopterygii"属,对于"Hoplobatrachus occipitalis"物种,我也想这样做

qgelzfjb

qgelzfjb1#

这听起来像是您只是尝试更改两个特定值?如果是这样,我将使用base R子集:

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

这种方法的一个优点是,它可以更清楚地表明您只是在调整一些特定的错误,而不是归纳为一组规则

sczxawaw

sczxawaw2#

我不确定是否理解了您的目标,但以下是dplyr::case_when的主要思想

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

33qvvth13#

试试这个:

重命名菌种1

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

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

组 Dataframe

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

相关问题