使用R中的for循环内的if语句将字符串组折叠成一个字符串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用R中的for循环内的if语句将字符串组折叠成一个字符串相关的知识,希望对你有一定的参考价值。

我有一个带有“Food”列的数据框。

dataframe <- data.frame(Color = c("red","red","red","red","red","blue","blue","blue","blue","blue","green","green","green","green","green","orange","orange","orange","orange","orange"), 
Food = c("banana","apple","potato","orange","egg","strawberry","cheese","yogurt","kiwi","butter","kale","sugar","carrot","celery","radish","cereal","milk","blueberry","squash","lemon"), Count = c(2,5,4,8,10,7,5,6,9,11,1,8,5,3,7,9,2,3,6,4))

每次出现水果,我都想用“水果”代替水果的名称。

我已经尝试制作水果名称的矢量。然后我遍历数据框中的每一行,字符串与水果匹配,我想用“水果”替换水果名称。

fruit_list <- c("banana","apple","orange","strawberry","kiwi","blueberry","lemon")

for (r in 1:nrow(dataframe)) {
  for (i in 1:length(fruit_list)){
    if (length(grep(fruit_list[i], dataframe$Food[r])) != 0) { 
      dataframe$Food[r] <- paste("fruit") 
    }
  }
}

我如何使用这种通用格式,以便数据框$ Food最终不会填充NA?

答案

只有R基:

dataframe$Food <- 
sapply(dataframe$Food,
        function(x,fruit_list) ifelse(x %in% fruit_list, "fruit", as.character(x) ),
        fruit_list = fruit_list ) 
另一答案

使用dplyr

  library(dplyr)
    ataframe %>% 
      mutate(Food=as.character(Food),
             Food=ifelse(Food%in%fruit_list,"Fruit",Food))#can change to fruit

结果:

 Color   Food Count
1     red  Fruit     2
2     red  Fruit     5
3     red potato     4
4     red  Fruit     8
5     red    egg    10
6    blue  Fruit     7
7    blue cheese     5
8    blue yogurt     6
9    blue  Fruit     9
10   blue butter    11
11  green   kale     1
12  green  sugar     8
13  green carrot     5
14  green celery     3
15  green radish     7
16 orange cereal     9
17 orange   milk     2
18 orange  Fruit     3
19 orange squash     6
20 orange  Fruit     4
另一答案

你不一定需要dplyr。只需使用:

dataframe$Food <- ifelse(dataframe$Food %in% fruit_list, "Fruit", as.character(dataframe$Food))
另一答案

您可以使用data.table包在一行中完成此操作 -

> setDT(dataframe)[,Food:=ifelse(Food %in% fruit_list,"fruit",as.character(Food))]

Color   Food Count
 1:    red  fruit     2
 2:    red  fruit     5
 3:    red potato     4
 4:    red  fruit     8
 5:    red    egg    10
 6:   blue  fruit     7
 7:   blue cheese     5
 8:   blue yogurt     6
 9:   blue  fruit     9
10:   blue butter    11
11:  green   kale     1
12:  green  sugar     8
13:  green carrot     5
14:  green celery     3
15:  green radish     7
16: orange cereal     9
17: orange   milk     2
18: orange  fruit     3
19: orange squash     6
20: orange  fruit     4

以上是关于使用R中的for循环内的if语句将字符串组折叠成一个字符串的主要内容,如果未能解决你的问题,请参考以下文章

使用 R 中的 for 和 if 语句在fluidRow() 中进行迭代

如何在 R 中保存循环(使用 if 语句)的结果?

shell 中的forwhile循环及if语句

JavaScript基础 if 循环 for 循环

如何在R语言中用循环语句求一列中每24个数的均值

R语言使用Repeat函数多次执行代码块内的语句,实现循环执行任务的功能:repeat没有提供任何检查条件,所以编码者必须给出退出重复循环的条件(一般使用if和break)