比较列值和NA

时间:2019-08-21 20:09:24

标签: r dataframe

下面的输入表中的数据点为NA。我在R中了解到,将值与NA进行比较会导致输出为NA

还有没有办法解决,所以我仍然可以使用ifelse()进行比较,并确保所比较的值之一是NA,那么它仍然会像NA一样提供输出是要比较的字符/字符串吗?

输入原始数据

data <- read.table(header = TRUE, text = "A B
                    NA  TEST
                   TEST TEST
                   Abaxasdas Test")

输入数据表

 A       B
<NA>   TEST
TEST   TEST
Abaxasdas Test

代码

data$Output <- ifelse(as.character(data$A) == as.character(data$B), "YES", "NO")

输出

 A    B   Output
<NA> TEST  <NA>
TEST TEST  YES
Abaxasdas Test NO

预期产量

 A    B   Output
<NA> TEST  NO
TEST TEST  YES
Abaxasdas Test NO

3 个答案:

答案 0 :(得分:5)

为简化起见,让我们首先使用stringsAsFactors=FALSE重新定义数据框:

df <- read.table(header = TRUE, text = "A B
                     NA  TEST
                    TEST TEST
                    Abaxasdas Test", stringsAsFactors=FALSE)

您可以使用NAidentical安全的方式比较列:

mapply(identical, df$A, df$B)

要使用“是”和“否”而不是TRUEFALSE来获得输出:

ifelse(mapply(identical, df$A, df$B), "YES", "NO")

输出

> df$Output <- ifelse(mapply(identical, df$A, df$B), "YES", "NO")
> df
          A    B Output
1      <NA> TEST     NO
2      TEST TEST    YES
3 Abaxasdas Test     NO

替代

正如joran在评论中所建议的那样,用值替换NA将使比较容易。如果您不想更改数据框中的值(但也许应该!),则可以使用如下帮助函数:

rna <- function(x) replace(x, is.na(x), "")
ifelse(rna(df$A)==rna(df$B), "YES", "NO")

答案 1 :(得分:3)

您可以使用dplyr中的from tkinter import * from tkinter import simpledialog as sd from datetime import datetime, date import time import pyowm as owm # access API key owm = foo COUNTRY = 'US' class App(Tk): def __init__(self): # create window and set size self.root = Tk() self.root.title("Weatherman") self.root.geometry('700x200') # get location self.location = sd.askstring("Input","What's your location? Format as City, Country",parent=self.root) self.location.strip() # get date and time current_date = StringVar() current_date.set(date.today) # debug label to check variable self.test_label = Label(self.root,text=current_date.get()) self.test_label.pack() def main(): app = App() app.mainloop if __name__ == '__main__': main() 。将第一种情况的输出调整为您想要的任何内容。

case_when

答案 2 :(得分:2)

@lebatsnok有一个很好的答案。 如果我们不需要ifelse,我会这样做:

data <- read.table(header = TRUE, text = "A B
                        NA  TEST
                       TEST TEST
                       Abaxasdas Test")

    data$output <- NA
    data$output[as.character(data$A) == as.character(data$B)] <- "YES"
    data$output[as.character(data$A) != as.character(data$B)] <- "NO"
    data$output[is.na(as.character(data$A)) | is.na(as.character(data$B))] <- "NO"

> data
          A    B output
1      <NA> TEST     NO
2      TEST TEST    YES
3 Abaxasdas Test     NO