确认两行包含数据

时间:2018-06-18 08:56:43

标签: r

如果我有一个如下表

> dput(Stackoverflowexcelexample)
structure(list(Column.1 = c("Row 1", "Row 2", "Row 3", "Row 4", 
"Row 5", "Row 6", "Row 7", "Row 8", "Row 9", "Row 10", "Row 11"
), Column.2 = c("data", "data", "data", "data", "data", "data", 
"data", "data", "data", "data", "data"), Column.3 = c(42989, 
42989, 42989, 42989, 42989, 42989, 42989, 42989, 42989, 42989, 
42989), Column.4 = c(3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14), Column.5 = c(1, 
2, 3, 3, 3, 3, 3, 3, 3, 3, 3), Column.6 = c(1, 2, 3, 4, 5, 6, 
7, 8, 9, 10, 11), Column.7 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0), Column.8 = c("data here", NA, NA, NA, NA, "data here", NA, 
NA, "data here", "data here", "data here"), Column.9 = c(42986, 
NA, NA, NA, NA, 42983, NA, NA, 42983, 42985, 42983)), row.names = c(NA, 
11L), class = "data.frame")

(注意:诸如42989之类的值为dates

如何确认Column 8行中是否有值,date中的Column 9值必须为if,反之亦然?我认为它应该在source('~/examplescript.R') [1] "Missing Value" 声明中,但我不确定。

预期输出

我们假设给出了这个:This is given.

在第5行中,第9列中有“日期”,第8列中有NA。

然后它应该打印到控制台(“缺失值”)

source('~/examplescript.R')
[1] "Missing Date"

如果第8列第5行中有数据但第9列中没有日期,则应打印缺失日期。

try {
     Shell shell = new Shell(Display.getDefault());
     Composite displayArea = new Composite(shell, SWT.BORDER);
     displayArea.setLayout(new FillLayout());
     OleFrame oleFrame = new OleFrame(displayArea, SWT.NONE);
     OleClientSite clientSite = new OleClientSite(oleFrame, SWT.NONE, "Word.Document", new File(fileName));
} catch (SWTException error) {
     error.printStackTrace();
}

请注意:第5行只是一个示例行。这应该适用于任何行,但只打印一次预期的输出。

1 个答案:

答案 0 :(得分:1)

您可以尝试将一个简单的ifelse语句与'rowSums'结合使用。这个想法很简单,并指出如果没有NA,则NAs的总和将为0,即

ifelse(rowSums(is.na(df[8:9])) > 0, 'Missing Value', 'Valid Data')

#                  1               2               3               4               5               6               7               8 
#       "Valid Data" "Missing Value" "Missing Value" "Missing Value" "Missing Value"    "Valid Data" "Missing Value" "Missing Value" 
#                 9              10              11 
#       "Valid Data"    "Valid Data"    "Valid Data" 

另一个输出可能是,

paste0('value: ', ifelse(is.na(df$Column.8), 'Missing Value', 'Valid'), ' - Date: ', ifelse(is.na(df$Column.9), 'Missing Date', 'Valid'))
 #[1] "value: Valid - Date: Valid"                "value: Missing Value - Date: Missing Date" "value: Missing Value - Date: Missing Date"
 #[4] "value: Missing Value - Date: Missing Date" "value: Missing Value - Date: Missing Date" "value: Valid - Date: Valid"               
 #[7] "value: Missing Value - Date: Missing Date" "value: Missing Value - Date: Missing Date" "value: Valid - Date: Valid"               
#[10] "value: Valid - Date: Valid"                "value: Valid - Date: Valid"