我试图在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
但是如何在没有引号namezz(Primus,data,Name)
?的情况下通过Primus?在我的问题中考虑某事,但as.character(quote())
不起作用..
答案 0 :(得分:3)
有人可以对此纠正我,但我认为你将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
传递数据=数据的问题是explained very nicely here。该帖子的摘录(他们提到你会引用数据的testparams)......
“关于评估的最重要的事情之一 函数的参数是 提供的参数和默认参数的处理方式不同。 提供的参数 在调用的评估框架中评估函数 功能。默认参数 在函数的评估框架中评估函数。“
参数testparams,当没有传递匹配的参数时,给出 默认值,即变量testparams的值 在定义了foo的环境中查找 not ,并且不 in 调用foo的环境,而不是本地环境 调用函数时创建的以及参数映射到的位置 值 - 在这种环境中,testparams是一个参数,即 已经在评估中,因此递归查找错误。