I have a dataframe.
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)
I have an input text file and an output text file. Input file has a field named "CONTENT". From the data frame given above I am passing the sentence through a loop and performing some calculations. In the output file I have a field named "Score", where the a score will be populated. I have to extract the score and store it in an object.
I have written the following codes.
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)
}
The output file looks like this:
c("Content = @_ShankarNath Hey Shankar, thank you for highlighting this to us, it will be taken care.",
"Delimiter = ", "Score = 1.978", "Result = Success")
The value of the score get replaced with every iteration, and I am trying to capture the same, before the loop moves on to the second line.
The print score return the values for all the statements. However when I try replacing the
print(s) with s the function is returning NULL. I tried using return(s) I am getting an error Error: No function to return from, jumping to the top level.
Not sure where I am going wrong.
s[i] <- get.score(a), and keeps <- list()outside thefor loop?