我可以使用stringr在第一行找到开始的"http“位置,
library(stringr)
a <- str_locate(message[1,], "http")[1]
a
[1] 38我想找到每一行的起始位置,并使用"apply“函数:
message$location <- apply(message, 1, function(x) str_locate(message[x,], "http")[1]) 但是它显示了所有的"NA“值,我能修复它吗?
发布于 2015-12-09 11:03:02
因为我们使用匿名函数调用(function(x)),所以可以使用每行的输入变量作为x,即str_locate(x, ...)
apply(message, 1, function(x) str_locate(x, "http")[1])
#[1] 11 16或者不指定匿名函数
apply(message, 1, str_locate, pattern="http")[1,]
#[1] 11 16正如@thelatemail提到的,如果我们只在第一列中寻找模式,我们不需要apply循环。
str_locate(message[,1], "http")[,1]
#[1] 11 16数据
message <- data.frame(V1= c("Something http://www...",
"Something else http://www.."), v2= 1:2)https://stackoverflow.com/questions/34170007
复制相似问题