简体   繁体   English

如何从函数的输出中创建打印值?

[英]How to create a print value from output of function?

I have this dataframe: 我有这个数据框:

df <- data.frame(ID=rep(33,5),
                 Number=1:5,
                 Time=c(2.00,1.98,0.82,2.12,2.53),
                 Distance=c(870,859,305,651,502))

I wanted to filter it by Time so I used this code(thanks to a previous post): 我想按时间过滤它,所以我使用了这段代码(感谢上一篇文章):

getHLN <- function(df, ID) {
  df %>%
    filter (ID ==id & (Number %in% 1:3 & (Time < 0.90))|
           (ID == id & (Number %in% 4:5 & (Time > 2.10))))
}

Which now gives this output: 现在给出以下输出:

  ID Number Time Distance
1 33      3 0.82      305
2 33      4 2.12      651
3 33      5 2.53      502

I'm wondering if I would be able to create or edit my current function to produce an output that has an output printing -3, +4, +5 instead to differentiate between less than and greater more easily? 我想知道是否可以创建或编辑当前函数以生成输出为-3,+ 4,+ 5的输出,从而更容易区分小于和大于?

If we need a character column, then use mutate to modify the 'Number' column 如果需要character列,请使用mutate修改“数字”列

getHLN <- function(df, id) {
 df %>%
    filter(ID ==id & (Number %in% 1:3 & (Time < 0.90))|
       (ID == id & (Number %in% 4:5 & (Time > 2.10)))) %>%
    mutate(Number = ifelse(Time < 0.90, paste0("-", Number), paste0("+", Number)))

 }

getHLN(df, 33)
#   ID Number Time Distance
#1 33     -3 0.82      305
#2 33     +4 2.12      651
#3 33     +5 2.53      502

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM