r - 将结果写回 lastrow + 1 到新创建的数据帧
问题描述
我正在尝试将 for 循环中的结果写回我创建的新数据帧。我想遍历每一行,然后将结果写入数据框的最后一行(lastrow + 1)。不幸的是,我被卡住了,无法将结果写回。
#create a new dataframe
results<- data.frame(Fields=character(), Employee=character(), Plan=character())
a<-COMP30$PFRPrint_Has_Plan
b<-COMP30$PFRPrint_Is_Eligible
c<-COMP30$`Employee ID`
d<-results$Employee
matches<-cbind(a,b,c)
res<-cbind(d)
for (k in 1:nrow(matches)){
if (matches[k,1]==TRUE & matches[k,2]==FALSE){
results[nrow(results$Employee)+1,1]<-matches[k,3]
}
}
解决方案
如果我已经正确理解了您要执行的操作,那么您正在尝试过滤COMP30$`Employee ID`
到那些情况,其中COMP30$PFRPrint_Has_Plan == TRUE
和COMP30$PFRPrint_Is_Eligible == FALSE
.
那么为什么不干脆去
library(dplyr)
results <- COMP30 %>%
filter(PFRPrint_Has_Plan,
!PFRPrint_Is_Eligible) %>%
select("Employee ID") # Might need backticks for that column name
由于您的循环没有为Fields
or列指定值,因此我假设您稍后Plan
将它们添加到results
推荐阅读
- fortran - 如何在 Fortran 中获得柯西分布?
- android - 从 Android 中的任何 Fragment 捕获 MainActivity 中的键盘按下事件
- c++ - 朋友类可以从 C++ 中的朋友类创建对象吗?
- android - 我可以在导航组件中使用多个 NavHostFragment 吗?
- ms-access-2010 - 代码添加另一条记录而不是更新它
- php - Mysqli::begin_transaction 在 php fork 中失败
- javascript - CKEditor 5 功能开启/关闭标题
- java - 如何验证 Java 的 git 提交消息?
- python - python: sub() 函数用法
- python - doc2vec 不准确的余弦相似度