r - How to find rows IDs that a value exists in any of column in R and then put Exists to a new dataframe
问题描述
want to find to which rows but in any column,there is a dataframe
解决方案
One option is rowSums
on a logical matrix created with ==
, then convert it to a logical vector with > 0
, wrap with which
to return the row index where atleast one column have 'citrus fruit' value. Use that index to change the value in 'newcol' of the dataset 'groc' to 'exists'
i1 <- which(rowSums(A == 'citrus fruit') > 0)
groc$newcol[i1] <- 'exists'
data
set.seed(24)
A <- data.frame(col1 = c('citrus fruit', 'a', 'b'), col2 = c('a', 'b',
'citrus fruit'), stringsAsFactors = FALSE)
groc <- data.frame(value = rnorm(3))
推荐阅读
- python - Django ModuleNotFoundError:没有名为“ui”的模块
- hyperledger - Hyperledger Iroha - 运行示例代码时出现 MST_EXPIRED 错误
- javascript - 无法使用 Sockets 在聊天应用程序中发送消息?
- python - 如何使 pip 在 Windows 上可用于 git bash 命令行?
- android - 安卓电话类型解读
- java - GridBagLayout 中的项目未正确对齐
- c - gcc execstack 标志究竟允许哪些情况以及它是如何执行的?
- amazon-web-services - Amazon S3 存储桶策略公共访问被拒绝
- sql-server - 将数据库中的所有 datetime 列转换为 datetime2 有什么危险
- c - 为什么调用 AddNode() 函数后大小等于二叉树中添加的数据?