简体   繁体   English

在至少一个 data.frame 中输出包含 NA 的行名称

[英]output row names containing NA in atleast one data.frame

I have two dataframes:我有两个数据框:

df1: df1:

        c1-1       c2-45      c3-65      c4-88         c5-97
r1        NA    0.598817857 0.053798422 0.776829475      NA
r2  0.481191121 0.67205121  0.50231424  0.933501988 0.169838618
r3  0.127680486       NA    0.188186772       NA    0.410198769
r4  0.448870194 0.372560979 0.627946034 0.277422856 0.540501786
r5  0.828152448 0.962372344 0.72686092  0.881644452 0.822969723

df2: df2:

          c1-24     c2-98     c3-77        c4-82       c5-9
r1  0.528260595 0.602697657 0.15193253  0.458712206 0.785602995
r2  0.250479754 0.999715659 0.575051699     NA      0.830962509
r3     NA           NA      0.733031402 0.189934875 0.554902551
r4  0.160801532 0.611729999 0.665725625 0.966146299 0.005503371
r5  0.483603251 0.306977032 0.377184726 0.109827232 0.63159439

both of them contain the same row names, but contain different column names (the string before the '-' symbol is the same for both dataframes but the string after is different).它们都包含相同的行名,但包含不同的列名('-' 符号之前的字符串对于两个数据帧是相同的,但后面的字符串不同)。

I would like to compare the two dataframes and output rows that contain NA in atleast one of them.我想比较两个数据帧和至少在其中一个中包含 NA 的输出行。 for example: the output in the above example would be:例如:上面例子中的输出是:

r1, r2, r3 r1、r2、r3

  • is.na to check for NA values is.na检查NA
  • Use |使用| to get TRUE if either df1 or df2 has NA .如果df1df2具有NA则为TRUE
  • rowSums to count NA values in the row rowSums计算行中的NA
  • return rownames of only those rows that have more than 0 NA values.仅返回具有 0 个以上NA值的那些行的行名。
rownames(df1)[rowSums(is.na(df1) | is.na(df2)) > 0]
#[1] "r1" "r2" "r3"

We can use我们可以用

row.names(df1)[Reduce(`|`, lapply(df1 * df2, is.na))]

-output -输出

[1] "r1" "r2" "r3"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 沿data.frame的行的总和,R中为NA - cumsum along row of data.frame with NA in R 从某些字符串中删除 NA 并将其粘贴到一个 data.frame 中 - Remove NA from certain strings and glue it into one data.frame 将列表转换为一行data.frame - Converting a list to one row data.frame rbind两个data.frame保留行顺序和行名称 - rbind two data.frame preserving row order and row names 列出 R 中 data.frame 中缺失数据的名称和行号? - List the names and row numbers of missing data in a data.frame in R? 初始化具有与现有data.frame相同的列名和行名的空白data.frame - Initializing a blank data.frame with the same column and row names as existing data.frame 使用另一个data.frame的行名对data.frame进行排序 - Sort a data.frame using row names of another data.frame 提取 data.frame 中与另一个 data.frame 中的值匹配的所有 row.names - Extract all row.names in a data.frame that match a value in another data.frame 将3x3 data.frame转换为1x9 data.frame,同时保留行和列名称 - Turn 3x3 data.frame into 1x9 data.frame while preserving row and column names 如何从 2 个向量创建一行 data.frame(tibble)。 一个具有所需的列名,另一个具有行的值? - How to create a one-row data.frame (tibble) from 2 vectors. One with the desired column names, and another with the values for the row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM