如何在R中创建多个新的逻辑列(基于数字变量的截止序列)?
我正在分析生存数据,我希望能够根据1:24的阈值来计算逻辑向量,并将其放在数据框架中。
例如,获取此示例数据:
set.seed(1988)
test <- tibble(
survival = c(sample(1:40,10, replace =T))
)
我想迅速创建名为“ rencevival1”,“ rence2”等的新列,直至“ rence24” - 具有逻辑值,基于生存&GT;临界点。
由于我对Dplyr最熟悉,到目前为止,我一直在手动突变,
test %>% mutate(survival1 = survival > 1, survival2 = survival > 2)
但是我认为必须有更好的方法!
I'm analysing survival data, and i am hoping to take a column of survival durations and calculate logical vectors based on a threshold of 1:24 - and put this together in a dataframe.
For example, take this sample data:
set.seed(1988)
test <- tibble(
survival = c(sample(1:40,10, replace =T))
)
I would like to rapidly create new columns titled "survival1","survival2" etc - up to "survival24" - with logical values based on whether survival > threshold.
As i'm most familiar with dplyr I've thus far been manually mutating e.g.
test %>% mutate(survival1 = survival > 1, survival2 = survival > 2)
But i thought there must be a better way!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法弄清楚如何在一个步骤中创建和命名列,但是就我所获得的而言:
在2022-07-08创建的 reprex package (v2.0.1)
I can't figure out how to create and name the columns in a single step, but here is as far as I got:
Created on 2022-07-08 by the reprex package (v2.0.1)
使用基本
R
Using base
R