1

我正在尝试在 package.json 的帮助下编写一个在名称中查找模式的函数stringr。我的功能如下所示:

namezz=function(thepatternx,data=data,column=Name){

  library(stringr)

  thepattern=as.character(quote(thepatternx))

  pattern <- thepattern
  strings <- data$column ##data$column is a character vector
  found=str_detect(strings, pattern)
  yez= rownames(data[which(found==TRUE),])
  hhh=as.numeric(yez)+1
  return(hhh)

}

当我调用该函数时,出现错误:

namezz(Primus)

Error in namezz(Primus) : 
  promise already under evaluation: recursive default argument reference or earlier problems?

无法理解错误,以及我做错了什么..提前感谢您提供的任何指导方针:)

EDIT: 如果我改为这样写:

namezz=function(thepatternx,data,Name){

  library(stringr)

  thepattern=as.character(quote(thepatternx))

  pattern <- thepattern
  strings <- data$Name  #####data$column is a character vector
  found=str_detect(strings, pattern)
  yez= rownames(data[which(found==TRUE),])
  hhh=as.numeric(yez)+1
  return(hhh)

}

我得到:

namezz(Primus,data,Name)

numeric(0)

这是不正确的,因为如果我执行该过程而不将其放入函数中,我会得到以下行:

pattern="Primus"
strings <- data$Name
mja=str_detect(strings, pattern)
yez= rownames(data[which(mja==TRUE),])
hhh=as.numeric(yez)+1

    [1] 2 3 4 5 6 7 8 9

这是一个输入:

dput(head(data))
structure(list(Year = 1901:1906, Name = c(">>Primus<< sbk", ">>Primus<< sbk", 
">>Primus<< sbk", ">>Primus<< sbk", ">>Primus<< sbk", ">>Primus<< sbk"
), Established = c(1899L, 1899L, 1899L, 1899L, 1899L, 1899L), 
    Bolagskod = c(2L, 2L, 2L, 2L, 2L, 2L), Kategori = c(2L, 0L, 
    0L, 0L, 0L, 0L), BranschTillhörighet = c(2L, 2L, 2L, 2L, 
    2L, 2L), Startår = c(1901L, 1901L, 1901L, 1901L, 1901L, 1901L
    ), Stoppår = c(1908L, 1908L, 1908L, 1908L, 1908L, 1908L), 
    Ranges = c("8  1901 - 1908  >>Primus<< sbk", "8  1901 - 1908  >>Primus<< sbk", 
    "8  1901 - 1908  >>Primus<< sbk", "8  1901 - 1908  >>Primus<< sbk", 
    "8  1901 - 1908  >>Primus<< sbk", "8  1901 - 1908  >>Primus<< sbk"
    ), Years.present = c("1901  1902  1903  1904  1905  1906  1907  1908", 
    "1901  1902  1903  1904  1905  1906  1907  1908", "1901  1902  1903  1904  1905  1906  1907  1908", 
    "1901  1902  1903  1904  1905  1906  1907  1908", "1901  1902  1903  1904  1905  1906  1907  1908", 
    "1901  1902  1903  1904  1905  1906  1907  1908"), Delägare.män. = c(267L, 
    271L, 317L, 339L, 339L, 345L), Delägare.kvinnor. = c(246L, 
    251L, 236L, 244L, 260L, NA), Sjukdomsfall.män. = c(66L, 61L, 
    100L, 103L, 106L, 82L), Sjukdomsfall.kvinnor. = c(59L, 55L, 
    60L, 71L, 85L, 60L), Sjukdagar.män. = c(1686L, 1918L, 2149L, 
    2212L, 2331L, 1890L), Sjukdagar.kvinnor. = c(1681L, 1197L, 
    1589L, 1904L, 2282L, 1750L), Inkomster.InträdesAvgifter. = c(303L, 
    NA, NA, NA, NA, NA), Inkomster.RegelbundnaAvgifter. = c(4901L, 
    4939L, 5172L, 5687L, 5728L, 5879L), Inkomster.UtdebiteradeAvgifter. = c(1313L, 
    1045L, 1141L, 2024L, 1462L, 1934L), Inkomster.Böter. = c(241L, 
    NA, NA, NA, NA, NA), SummaMedl.avg. = c(6758L, 5984L, 6313L, 
    7711L, 7190L, 7813L), Inkomster.BidragStatKommun. = c(366L, 
    440L, 456L, 464L, 476L, 493L), Inkomster.Räntor. = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), Inkomster.Övrigt. = c(24L, 722L, 874L, 605L, 805L, 647L
    ), Inkomster.Summa. = c(7148L, 7146L, 7644L, 8781L, 8472L, 
    8954L), DiffIntäkter.SummaMotVerkligSumma. = c(0L, 0L, -1L, 
    -1L, -1L, -1L), Utgifter.Sjukhjälp. = c(4735L, 4450L, 5300L, 
    5870L, 6560L, 5200L), Utgifter.Begravningshjälp. = c(1200L, 
    795L, 1045L, 1810L, 955L, 1675L), Utgifter.Arvoden. = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), Utgifter.Förvaltning. = c(956L, 972L, 1038L, 1156L, 1523L, 
    1171L), Utgifter.Övrigt. = c(25L, NA, 20L, 5L, NA, NA), Utgifter.Behållning. = c(231, 
    929, 240, -59, -565, 908), Utgifter.SummaÖvrigt.Behållning. = c(256L, 
    929L, 260L, -54L, -565L, 908L), Utgifter.Summa. = c(7148L, 
    6217L, 7403L, 8841L, 9038L, 8046L), KOLL = c(-1L, 0L, 0L, 
    0L, 0L, 0L), Tillgångar.KontantIKassa. = c(835L, 1765L, 2006L, 
    1946L, 1380L, 2259L), Tillgångar.KontantMedelBank. = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), Tillgångar.Totalt. = c(836L, 1765L, 2006L, 1946L, 1468L, 
    2348L), Skulder.Totalt. = c(NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_), TillgångarÖverSkulder = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    )), .Names = c("Year", "Name", "Established", "Bolagskod", 
"Kategori", "BranschTillhörighet", "Startår", "Stoppår", "Ranges", 
"Years.present", "Delägare.män.", "Delägare.kvinnor.", "Sjukdomsfall.män.", 
"Sjukdomsfall.kvinnor.", "Sjukdagar.män.", "Sjukdagar.kvinnor.", 
"Inkomster.InträdesAvgifter.", "Inkomster.RegelbundnaAvgifter.", 
"Inkomster.UtdebiteradeAvgifter.", "Inkomster.Böter.", "SummaMedl.avg.", 
"Inkomster.BidragStatKommun.", "Inkomster.Räntor.", "Inkomster.Övrigt.", 
"Inkomster.Summa.", "DiffIntäkter.SummaMotVerkligSumma.", "Utgifter.Sjukhjälp.", 
"Utgifter.Begravningshjälp.", "Utgifter.Arvoden.", "Utgifter.Förvaltning.", 
"Utgifter.Övrigt.", "Utgifter.Behållning.", "Utgifter.SummaÖvrigt.Behållning.", 
"Utgifter.Summa.", "KOLL", "Tillgångar.KontantIKassa.", "Tillgångar.KontantMedelBank.", 
"Tillgångar.Totalt.", "Skulder.Totalt.", "TillgångarÖverSkulder"
), row.names = c(NA, 6L), class = "data.frame")

Edit

这有效:

namezz=function(thepatternx,data,Name){

  library(stringr)

  thepattern=thepatternx

  pattern <- thepattern
  strings <- data$Name
  mja=str_detect(strings, pattern)
  yez= rownames(data[which(mja==TRUE),])
  hhh=as.numeric(yez)+1
  return(hhh)  

} 

namezz("Primus",data,Name)
[1] 2 3 4 5 6 7 8 9

但是我怎么能在没有引号的情况下通过 Primus namezz(Primus,data,Name),?像我的问题一样思考一些事情,但as.character(quote())不起作用..

4

1 回答 1

4

有人可以纠正我,但我认为您将 Primus 和 Name 作为对象传递给函数,它正在 .GlobalEnv 中查找这些对象并且没有找到它们,因此您的函数无法执行您的大部分指令(并且什么都不返回)。我已经编辑了你的功能。

而是试试这个...

 namezz <- function( pattern = " ", data , column= "Name" ){
   library(stringr)
   strings <- data[ , column ] ##data$column is a character vector
   found = str_detect( strings , pattern )
   yez = rownames( data[ which( found==TRUE ) , ] )
   hhh = as.numeric( yez ) + 1
   return( hhh )
 }

然后你必须像这样使用函数:

namezz( "Primus" , data = data ) #In this case the default for column is "Name" as you want

传递 data=data 的问题在这里得到了很好的解释。该帖子的摘录(他们指的是testparams,您将指的是数据)......

“关于评估函数参数的最重要的事情之一是提供的参数和默认参数的处理方式不同。提供给函数的参数在调用函数的评估框架中进行评估。默认参数函数在函数的评估框架中进行评估。"

参数 testparams,当没有匹配的参数被传递时,被赋予默认值,即不是在定义 foo 的环境中查找的变量 testparams 的值,也不是在调用 foo 的环境中,而是在调用函数时创建的本地环境以及参数映射到值的位置——在此环境中,testparams 是一个参数,它已经在评估中,因此会出现递归查找错误。

于 2013-02-20T15:10:44.803 回答