在R中使用ifelse。

时间:2022-02-14 14:58:13

I am restructuring a dataset of species names. It has a column with latin names and column with trivial names when those are available. I would like to make a 3rd column which gives the trivial name when available, otherwise the latin name. Both trivial names and latin names are in factor-class. I have tried with an if-loop:

我正在重组一个物种名称的数据集。它有一个包含拉丁名称的列,还有一个包含普通名称的列。我想做第三列,在可用的时候给出平凡的名字,否则就是拉丁名。平凡的名字和拉丁语的名字都属于因素类。我试过一个if-loop:

  if(art2$trivname==""){  
    art2$artname=trivname   
    }else{  
      art2$artname=latname  
    }  

It gives me the correct trivnames, but only gives NA when supplying latin names.
And when I use ifelse I only get numbers.

它给出了正确的三角名,但只在提供拉丁名时给出NA。当我使用ifelse时,我只能得到数字。

As always, all help appreciated :)

一如既往,所有的帮助都令人感激:)

3 个解决方案

#1


4  

Example:

例子:

art <- data.frame(trivname = c("cat", "", "deer"), latname = c("cattus", "canis", "cervus"))
art$artname <- with(art, ifelse(trivname == "", as.character(latname), as.character(trivname)))
print(art)
#   trivname latname artname
# 1      cat  cattus     cat
# 2            canis   canis
# 3     deer  cervus    deer

(I think options(stringsAsFactors = FALSE) as default would be easier for most people, but there you go...)

(我认为对于大多数人来说,违约选项(stringsAsFactors = FALSE)会更容易一些,但你看……)

#2


0  

Getting only numbers suggests that you just need to add as.character to your assignments, and the if-else would probably work you also seem to not be referring to the data frame in the assignment?

只得到数字意味着你只需要添加as。你的作业中的字符,if-else也可以你似乎在作业中没有提到数据框架?

if(as.character(art2$trivname)==""){  
    art2$artname=as.character(art2$trivname)
    }else{  
      art2$artname=as.character(art2$latname)
    }  

Option 2: Using ifelse:

选项2:使用ifelse:

 art2$artname= ifelse(as.character(art2$trivname) == "", as.character(art2$latname),as.character(art2$trivname))

It is probably easier (and more "R-thonic" because it avoids the loop) just to assign artname to trivial across the board, then overwrite the blank ones with latname...

它可能更容易(而且更像“R-thonic”,因为它避免了循环),只需将artname分配给所有的平凡元素,然后用latname覆盖空白的元素……

art2 = art
art2$artname = as.character(art$trivname)
changeme = which(art2$artname=="")
art2$artname[changeme] = as.character(art$latname[changeme])

#3


0  

If art2 is the dataframe, and artname the new column, another possible solution:

如果art2是dataframe,而artname是新列,另一种可能的解决方案是:

art2$artname <- as.character(art2$trivname)
art2[art$artname == "",'artname'] <- as.character(art2[art2$artname == "", 'latname'])

And if you want factors in the new column:

如果你想在新列中加入因子

art2$artname <- as.factor(art2$artname)

#1


4  

Example:

例子:

art <- data.frame(trivname = c("cat", "", "deer"), latname = c("cattus", "canis", "cervus"))
art$artname <- with(art, ifelse(trivname == "", as.character(latname), as.character(trivname)))
print(art)
#   trivname latname artname
# 1      cat  cattus     cat
# 2            canis   canis
# 3     deer  cervus    deer

(I think options(stringsAsFactors = FALSE) as default would be easier for most people, but there you go...)

(我认为对于大多数人来说,违约选项(stringsAsFactors = FALSE)会更容易一些,但你看……)

#2


0  

Getting only numbers suggests that you just need to add as.character to your assignments, and the if-else would probably work you also seem to not be referring to the data frame in the assignment?

只得到数字意味着你只需要添加as。你的作业中的字符,if-else也可以你似乎在作业中没有提到数据框架?

if(as.character(art2$trivname)==""){  
    art2$artname=as.character(art2$trivname)
    }else{  
      art2$artname=as.character(art2$latname)
    }  

Option 2: Using ifelse:

选项2:使用ifelse:

 art2$artname= ifelse(as.character(art2$trivname) == "", as.character(art2$latname),as.character(art2$trivname))

It is probably easier (and more "R-thonic" because it avoids the loop) just to assign artname to trivial across the board, then overwrite the blank ones with latname...

它可能更容易(而且更像“R-thonic”,因为它避免了循环),只需将artname分配给所有的平凡元素,然后用latname覆盖空白的元素……

art2 = art
art2$artname = as.character(art$trivname)
changeme = which(art2$artname=="")
art2$artname[changeme] = as.character(art$latname[changeme])

#3


0  

If art2 is the dataframe, and artname the new column, another possible solution:

如果art2是dataframe,而artname是新列,另一种可能的解决方案是:

art2$artname <- as.character(art2$trivname)
art2[art$artname == "",'artname'] <- as.character(art2[art2$artname == "", 'latname'])

And if you want factors in the new column:

如果你想在新列中加入因子

art2$artname <- as.factor(art2$artname)