Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何矢量化具有嵌套for循环的字符串获取脚本?_String_R_Vectorization_Apply_Sapply - Fatal编程技术网

String 如何矢量化具有嵌套for循环的字符串获取脚本?

String 如何矢量化具有嵌套for循环的字符串获取脚本?,string,r,vectorization,apply,sapply,String,R,Vectorization,Apply,Sapply,我在R中有一个数据,如下所示 Text <- c("reuce FR563 323 aldk", "vard 432", "DK123 fg4d", "matten global height") ID <- c("S1", "S2", "S3", "S4") data <- data.frame(ID, Text) data$noofwords <- sapply(data$Text, str_count,"[[:space:]]") +1 data$Text <

我在R中有一个数据,如下所示

Text <- c("reuce FR563 323 aldk", "vard 432", "DK123 fg4d", "matten global height")
ID <- c("S1", "S2", "S3", "S4")
data <- data.frame(ID, Text)
data$noofwords <- sapply(data$Text, str_count,"[[:space:]]") +1
data$Text <- as.character(data$Text)
data$ID <- as.character(data$ID)


data
ID                 Text noofwords
1 S1 reuce FR563 323 aldk         4
2 S2             vard 432         2
3 S3           DK123 fg4d         2
4 S4 matten global height         3

Text这使用了
data.table
包,应该比较快。 一定要检查列类型,因为您给出的示例数据被转换为
factor
变量(因此我在重新创建它时使用了
stringsAsFactors=FALSE

require(data.table)

dt之所以出现“1”,是因为您通过
as.character
索引中的值从
因子
强制为
字符
,这具有创建基本因子代码的字符表示的效果(每列一级,因此每列都转换为
1
。当您使用
索引确定时,则使用
索引确定,这是1的来源。data.table解决方案适用于小文件,但在使用6000行小数据集运行时,给出
无法分配大小为638.3 Mb的向量,但给出以下evecseq(f_uuuu,len_uuuu,if(allow.cartesian)NULL else as.integer(max(nrow(x))中存在大数据错误的错误:连接18514行的结果;超过14402=max(nrow(x),nrow(i))。检查i中是否存在重复的键值,每个键值都会一次又一次地加入到x中的同一组中。如果可以,请尝试包括
j
,并将
by
(by而不是by)删除,以便j为每个组运行,以避免大的分配。
dt[,list(关键字=unlist(strsplit(Text,”),by=list(ID,Text)]
帮了我的忙。
keyword <- "keyword"
text <- "text"
ID <- "ID"
Index <- data.frame(keyword,text,ID)
Index[,1:3] <- as.character(Index[,1:3])

n <- nrow(data)
for (i in 1:n) {
  k <- data[i,"noofwords"]
  kwv <- str_split(data[i,"Text"], " ", n = Inf)
  kwv <- unlist(kwv, recursive = TRUE, use.names = FALSE)
  for (j in 1:k){
    kw <- kwv[j]
    tex <- (data[i,"Text"])
    nid <- (data[i, "ID"])
    Index <- rbind(Index, c(kw,tex,nid))
  }
}


Index
   keyword                 text ID
1        1                    1  1
2    reuce reuce FR563 323 aldk S1
3    FR563 reuce FR563 323 aldk S1
4      323 reuce FR563 323 aldk S1
5     aldk reuce FR563 323 aldk S1
6     vard             vard 432 S2
7      432             vard 432 S2
8    DK123           DK123 fg4d S3
9     fg4d           DK123 fg4d S3
10  matten matten global height S4
11  global matten global height S4
12  height matten global height S4
require(data.table)
dt <- data.table( data , key = "ID" )
dt[ dt[ , list( Keyword = unlist( strsplit( Text , " " ) ) ) , by = ID ] ]
#    ID                 Text Keyword
# 1: S1 reuce FR563 323 aldk   reuce
# 2: S1 reuce FR563 323 aldk   FR563
# 3: S1 reuce FR563 323 aldk     323
# 4: S1 reuce FR563 323 aldk    aldk
# 5: S2             vard 432    vard
# 6: S2             vard 432     432
# 7: S3           DK123 fg4d   DK123
# 8: S3           DK123 fg4d    fg4d
# 9: S4 matten global height  matten
#10: S4 matten global height  global
#11: S4 matten global height  height