r - R:如何对数据框中的每一列应用一个复杂的函数,为每一列输出新的数据框?
问题描述
我已经阅读了一些帖子,但到目前为止,它们都没有解决我的问题。这是我想做的事情:
我的输入数据:我有这个数据框,所有的行都是产品,所有的列都是产品审查的标准。目标是查看每个标准的通过/失败率,看看哪个通过了大多数,以及所有产品都难以满足的标准。
(好像我不能在这里复制粘贴表格,所以我在下面输入数据框的样子)
**Source** **sample_ID** **Standard_a** **Standard_amn** **Standard_df**
Product1 1 Yes Yes NA
Product1 2 Yes NO Yes
... ... Yes Yes NA
Product1 10 Yes Yes NO
Product2 1 Yes NO YES
... ... .. .... ...
Product2 8 Yes Yes Yes
Product3 1 Yes NA NA
.... Yes Yes YES
- 有一些关于产品抽样的规则,以及列条目的有效条目(是表示通过标准,否表示否,NA 表示不可用等)。我创建了一个函数来处理它。
- 但我的斗争是如何将我的功能应用于所有列。
这是我的一列(第 14 列)的函数,我试图使它成为一个循环或在所有列上自动使用的东西
#take product info columns 1:3 and the 14th standard column: named um9d_f6,
and create a clean dataframe of this column, I call it x
x <- appeals_clean %>%
#select column um9d_f6, I use it's position here, which is 14
select(1:3,14) %>%
#group by source (which is product)
group_by(source) %>%
#code NA and missing as 0: this are the specific coding rule for pass and fail
mutate(convert_num = ifelse(um9d_f6 %in% c('YES','NO','AC'),1,0),
cum_elig = cumsum(convert_num),
max_cum_elig = max(cum_elig)) %>%
#get rid of: 1.empty entries after max eligible is reached 2.not enough eligible files 3.after eligible files reach 10
filter(!((max_cum_elig == cum_elig & um9d_f6 == '')|max_cum_elig <8|(max_cum_elig>10 & cum_elig >10)))
# now with the clean data from column 14 (again, named um9d_f6, now I calculate the pass rate)
y <- x %>%
group_by(source) %>%
summarise(um9d_f6_n_Pass = sum(um9d_f6 %in% c('YES','AC')),
um9d_f6_Rate = n_Pass/10)
Y 输出数据框如下所示,
**Source** **um9d_f6_n_Pass** **um9d_f6_Rate**
Product1 10 100
Product2 8 80
Product3 8 80
Product2 8 80
Product3 10 100
我的难题是如何使用循环或其他函数为数据框中的每个标准列获取此 y 汇总数据框。这样我就不需要在每次使用我创建的这个函数时手动调整列位置和列名(大约有 60 个标准,所以希望我不需要手动执行此操作)...
我尝试过循环,但还没有弄清楚 1. 如何引用函数中的列 x 和 2. 如何根据输入列名命名 Y 中的新列。 最终我将离开加入所有那些新的输出数据框 Y,这就是为什么我需要命名来反映它的源列名。
任何建议/建议将不胜感激!
谢谢你
狮子座
解决方案
推荐阅读
- drop-down-menu - Google Colab - 下拉设置几乎在能够选择任何内容之前立即关闭
- firebase - Jest & Firebase Emulator - 等待触发函数被调用
- javascript - 选择值并转到下一个下拉菜单后,值会消失
- python - 我可以使用扩展文件的主文件中定义的函数吗?
- java - Spring Boot StackOverFlowError:空
- python-3.x - 某些列值不被识别为几何类型,即使它们是
- node.js - 使用nodejs在图像中查找图像
- c# - Asp.NetWebAPI 添加相关实体
- node.js - 微服务架构通用中使用 RabbitMQ 的 JWT 授权
- firebase - Firestore:集合组安全规则的文档 ID 变为空