r - R - New Dataframe from Column Counts?
问题描述
I have a dataframe that looks like this:
Dog Breed Dog.Distraction Resource.Guarding
1 Max BL N/A 1
2 Rover YL 1 N/A
3 Tina GR 1 N/A
I want a new dataframe that looks like:
Dog. Distraction Resource.Guarding
Count 2 1
What's the best way to do this? I can use sum() to find each individual column, but not sure how to convert to new df. Alternatively, I'm also ok with:
Count
Dog.Distraction 2
Resource.Guarding 1
Thank you!
解决方案
我们可以使用colSums
假设N/A
是NA
out <- colSums(df1[c('Dog.Distraction', 'Resource.Guarding')], na.rm = TRUE)
如果我们需要改变names
names(out) <- c("V1", "V2")
的输出colSums
是一个命名的vector
推荐阅读
- python - 如何一次检查多个变量的值?
- javascript - 为什么我不断收到“错误:必须提供 media_id 字段。” 使用 Twit NPM 时?
- java - 如何使用 Gradle 指定 JVM 选项?
- java - org.json.JSONException:字符 0 处的预期文字值
- java - Java 流 - 从地图列表中获取地图
- json - 如何将(Deno)服务器发送的 JSON 保存为 Kotlin 中的数据类模型?
- python - 如何使用 NumPy 制作二维锯齿状数组
- android - 如何在 Flutter 中实现应用内截图?
- python - 对于内部列表,将字符串分解为 3 个字符的首字母缩写词
- swift - 我可以使用 RxSwift 摆脱回调地狱吗?