【问题标题】:How do I want to write a function that will run based on the first letter of a line?如何编写一个基于行首字母运行的函数?
【发布时间】:2022-02-03 08:06:35
【问题描述】:

#R 我一直在尝试提出一个想法逻辑,请帮助我。

if first character of line = "H" run function Header{
  if first character of line = "P" run function Patient{
.....other
  }
}

行内函数

  If "H" is first character of line = 
   Header <- string::str_split(Hline,fixed('|')) # Hline = String to set to collect H line. 
 ID_H <- Header[1] # [1] = The first value is extracted from the line by '|'.
 Del_H <- Header[2]

根据读取的行的第一个字母,有五种开箱即用的函数。 如果你有一个很好的例子。 请帮我 最好的问候

【问题讨论】:

    标签: r function readline


    【解决方案1】:

    我会在readline() 周围创建一个包装器,您可以在其中提取输入的第一个字符并使用switch() 来提取相关函数。请注意,您不能在 R 中使用'str'[1] 来获取's',这只是返回'str' 的向量索引。我们可以使用substr()

    reader <- function() {
      x <- readline()
      x1 <- tolower(substr(x, 1, 1))
      switch(
        x1,
        h = cumsum(1:4),
        p = cumprod(1:4)
      )
    }
    
    reader()
    #> hardy
    #> [1]  1  3  6 10
    
    reader()
    #> plenty
    #> [1]  1  2  6 24
    
    

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      相关资源
      最近更新 更多