r - 在 R 中创建一个新的 cloumn
问题描述
我想在 r 数据框中创建一个新列。我想将其他列中的此列值作为此示例:
ID column1 column2
1 C C
2 B C
3 A A
4 C B
新列应在 2 列中包含任何可用的 C,并且其值应如下所示:
ID column1 column2 new column
1 C C Present
2 B C Present
3 A A Absent
4 C B Present
因为我只想要列中的 C 你能帮帮我吗?
解决方案
这也可以在基础 R 中使用:
DF$new_column <- Map(function(x, y) c("Absent", "Present")[any("C" %in% c(x, y)) + 1],
DF$column1, DF$column2)
DF
ID column1 column2 new_column
1 1 C C Present
2 2 B C Present
3 3 A A Absent
4 4 C B Present
推荐阅读
- python - 在 Django 中存储测验答案信息的 Cookie
- c++ - 可以进行static_cast吗
从双,分配到双被优化掉? - asp.net - 处理数据库请求的正确方法
- java - 如何在java二叉搜索树中将值打印为长字符串?
- java - 在调用时需要但未调用
- php - 生成算术测试
- angular - 类似 Visual Studio 2017 的 angular2-switcher 的扩展
- ruby-on-rails - Rails 5 - 通过按钮将绝对当前 url 保存到 cookie 中?
- react-native - 如何在 react-native 应用程序中使用 react-native-recaptcha-v3 实现可见或正常的 recaptcha?
- python - 如何以有效的方式以pythonic方式将每个元素嵌套列表到变量