将函数输出存储到R中的对象

时间:2016-02-17 06:31:17

标签: r for-loop

我有一个数据框。

structure(list(CONTENT = c("@_ShankarNath Hey Shankar, thank you for highlighting this to us, it will be taken care.", 
"#deals #Puma Cell Kilter Black Sneakers is selling cheaper at INR 3899 today https://t.co/n9wLwofVzz #jabong"
), MEDIA_PROVIDER = c("TWITTER", "TWITTER")), .Names = c("CONTENT", 
"MEDIA_PROVIDER"), class = "data.frame", row.names = 1:2)

我有一个输入文本文件和一个输出文本文件。输入文件有一个名为“CONTENT”的字段。从上面给出的数据框中,我通过循环传递句子并执行一些计算。在输出文件中,我有一个名为“Score”的字段,其中将填充一个分数。我必须提取分数并将其存储在一个对象中。

我写了以下代码。

sco <- for (i in 1:nrow(dfa)){
  s <- list()
  filecon <- file("input.txt")
  writeLines(c("Username = ABC","Password = 123",paste("Content = ", dfa$CONTENT[i]),"Delimiter =  "), filecon)
  close(filecon)

# perform all the calculations

  a <- readLines("output.txt")
  get.score <- function(scor) {
    score <- scor[grepl("Score = ", scor)]
      as.numeric(strsplit(score, "Score = ")[[1]][2])
  }
  s <- get.score(a)
  print(s)
  }

输出文件如下所示:

c("Content = @_ShankarNath Hey Shankar, thank you for highlighting this to us, it will be taken care.", 
"Delimiter =  ", "Score = 1.978", "Result = Success")

在循环移动到第二行之前,每次迭代都会替换得分的值,并且我试图捕获相同的值。

打印分数返回所有语句的值。但是,当我尝试更换 print(s) s NULL函数返回return(s)。我尝试使用Error: No function to return from, jumping to the top level我收到错误ul, ol { margin: initial; padding: initial; }

不确定我哪里出错了。

1 个答案:

答案 0 :(得分:1)

到目前为止,我没有对其进行测试,我建议使用applyforeach来自foreach包,因为它似乎是您想要编写的内容。

get.score <- function(scor) {
    score <- scor[grepl("Score = ", scor)]
    as.numeric(strsplit(score, "Score = ")[[1]][2])
}

sco <- apply(dfa, 1, function(v) {
             filecon <- file("input.txt")
             writeLines(c("Username = ABC","Password = 123",paste("Content = ", v['CONTENT']),"Delimiter =  "), filecon)
             close(filecon)

             # perform all the calculations
             a <- readLines("output.txt")
             get.score(a)
})

这将输出矩阵或向量,但根据get.score的输出,这可能不合适。您也可以使用lapply

sco <- lapply(dfa$CONTENT, function(v) {
             filecon <- file("input.txt")
             writeLines(c("Username = ABC","Password = 123",paste("Content = ", v),"Delimiter =  "), filecon)
             close(filecon)

             # perform all the calculations
             a <- readLines("output.txt")
             get.score(a)
})

最后,for结构对您来说比较熟悉,您可以尝试

library(foreach)
sco <- foreach(v=dfa$CONTENT) %do% {
             filecon <- file("input.txt")
             writeLines(c("Username = ABC","Password = 123",paste("Content = ", v),"Delimiter =  "), filecon)
             close(filecon)

             # perform all the calculations
             a <- readLines("output.txt")
             get.score(a)
}

并非foreach包允许您进行并行计算。