此代码按预期工作:
my_tbl<-tibble(var1=c("a","a","a"), var2=c("b","b","b"))
string_to_match_list<-list("b")
my_row<-my_tbl[2,]
my_tbl$var2[2]<-match_every_cell_start_and_modify_with_binary(my_tbl, my_row, string_to_match_list)
match_every_cell_start_and_modify_with_binary<-function(my_tbl, my_row, string_to_match_list)
{
match_bool<-any(sapply(my_row, startsWith, unlist(string_to_match_list))) #matches start only of the string by whole string.
if (match_bool)
{
return(1)
} else
{
return(0)
}
}
my_tbl
输出:
# A tibble: 3 x 2
var1 var2
<chr> <chr>
1 a b
2 a 1
3 a b
如果我从函数my_tbl
中删除参数match_every_cell_start_and_modify_with_binary
无效,但是我发现函数my_tbl
内部未使用。
问题:
为什么函数match_every_cell_start_and_modify_with_binary
需要my_tbl
作为参数?
是否可以删除my_tbl
作为函数match_every_cell_start_and_modify_with_binary
的参数,并且不影响其功能?