R: Using GraphNEL, term frequency of extracted keywords - r

I'm running the below code to extract key phrases from a raw data file. While i am successfully able to do it, i am not able to get the frequency or count of the extracted keywords which would help me understand the ranking of the occurrence of the keywords since i am using GraphNEL. Is there any way i can get the key phrase count? TIA.
ConstructTextGraph <- function(n)
{
word_graph <- new("graphNEL")
i <- 1
while (i < length(words) ) {
if ( IsSelectedWord(words[i]) ) {
links <- GetWordLinks(i,n)
if (links[1] != "") {
cat(i," ",words[i]," - ",paste(c(links),collapse=" "),"\n")
if ( length(which(nodes(word_graph)==words[i]))==0 ) {
word_graph <- addNode(words[i],word_graph)
}
for (j in 1:length(links)) {
if ( length(which(nodes(word_graph)==links[j]))==0 ) {
word_graph <- addNode(links[j],word_graph)
word_graph <- addEdge(words[i],links[j],word_graph,1)
}
else {
if ( length(which(edges(word_graph,links[j])[[1]]==words[i]))>0 ) {
prev_edge_weight <- as.numeric(edgeData(word_graph,words[i],links[j],"weight"))
edgeData(word_graph,words[i],links[j],"weight") <- prev_edge_weight+1
}
else {
word_graph <- addEdge(words[i],links[j],word_graph,1)
}
}
}
}
}
i <- i+1
}
word_graph
}
Please let me know if more information is needed.

Related

After add new code, things cannot knit out

I met a wired situation. After add new code The code cannot knit out.
function_name <- function (...)
{
output <- if (output_format == "list") {
evolved.ts
} else if (output_format == "tsibble") {
as.tsibble(evolved.ts)
}
return(output)
}
You could read the arguments with args <- list(...):
function_name <- function (...)
{
args <- list(...)
# Code
output <- if (args$output_format == "list") {
evolved.ts
} else if (output_format == "tsibble") {
as.tsibble(evolved.ts)
}
return(output)
}

Double sampling method in R

My initial code for double sampling is the following. I did only one sample.
# Data
samples<-matrix(NA,nrow = 12, ncol = 2000)
for (i in 1:12) {
samples[i,]<- rbinom(2000,1,prob = 0.05)
}
# Double Sampling Plan
accept<-rep(0,12)
for (i in 1:12) {
if (sum(samples[i,1:80])<=5){
accept[i]<-1
} else if (sum(samples[i,1:80]<=8) & sum(samples[i,1:80]>5) ) {
if (sum(samples[i,1:160])<=12) {
accept[i]<-1
}
}
}
sum(accept)
Since I generated randomly from Bernoulli, every time you run the code, the results will not be the same.
I want 100 repetitions of this double sample.
My solution:
nm=double(100)
for (j in 1:100){
# Data
samples<-matrix(NA,nrow = 12, ncol = 2000)
for (i in 1:12) {
samples[i,]<- rbinom(2000,1,prob = 0.05)
}
# Double Sampling Plan
accept<-rep(0,12)
for (i in 1:12) {
if (sum(samples[i,1:80])<=5){
accept[i]<-1
} else if (sum(samples[i,1:80]<=8) & sum(samples[i,1:80]>5) ) {
if (sum(samples[i,1:160])<=12) {
accept[i]<-1
}
}
}
nm[j]=sum(accept)
}
mean(nm)
What do you think?
If we follow the proposition of #Onyambu, we can embeded one simulation inside a function and call it in a loop like this :
one_double_sampling <- function(){
# Data
samples<-matrix(NA,nrow = 12, ncol = 2000)
for (i in 1:12) {
samples[i,]<- rbinom(2000,1,prob = 0.05)
}
# Double Sampling Plan
accept<-rep(0,12)
for (i in 1:12) {
if (sum(samples[i, 1:80])<=5){
accept[i]<-1
} else if (sum(samples[i,1:80]<=8) & sum(samples[i,1:80]>5) ) {
if (sum(samples[i,1:160])<=12) {
accept[i]<-1
}
}
}
return(sum(accept))
}
set.seed(123)
# number of sample
n <- 100
# stock the result
res <- rep(0, n)
for(i in 1:n){
res[i] <- one_double_sampling()
}
# mean
mean(res)
Definitly your code is correct. For people interresting by the double sampling method I advise you to see this.
Edit 1
In one line code based on Onyambu advise :
mean(replicate(n, one_double_sampling()))

R user-defined function called but not running

I am new to R, and I am trying to create a user-defined function and run it inside a for loop. The function is defined in the same script before it is called. However, when the function is called, the code inside the function doesn't run. No syntax errors occur.
Here is the function definition:
placeInVector <- function(subjectID, morpho) {
if (morpho == "Alg") {
algVec <- c(algVec, subjectID)
}
else if (morpho == "Anem") {
anemVec <- c(anemVec, subjectID)
}
else if (morpho == "Barn") {
barnVec <- c(barnVec, subjectID)
}
else if (morpho == "Biv") {
bivVec <- c(bivVec, subjectID)
}
else if (morpho == "BrBryo") {
brbryoVec <- c(brbryoVec, subjectID)
}
else if (morpho == "ColTuni") {
coltuniVec <- c(coltuniVec, subjectID)
}
else if (morpho == "EnBryo") {
enbryoVec <- c(enbryoVec, subjectID)
}
else if (morpho == "Nothing") {
nothingVec <- c(nothingVec, subjectID)
}
else if (morpho == "SolTuni") {
soltuniVec <- c(soltuniVec, subjectID)
}
else if (morpho == "Spng") {
spngVec <- c(spngVec, subjectID)
}
else if (morpho == "TubeWm") {
tubewmVec <- c(tubewmVec, subjectID)
}
}
Here is where the function is called later on in the script:
for (subject in subjects$SubjectID) {
currentRow <- finalIDs %>%
filter(SubjectID == subject)
morphoID <- as.character(currentRow$FinalIDs1)
if (morphoID != "NONE") {
placeInVector(subject, morphoID)
}
}
Note: all of the vectors referenced in the function definition (algVec, anemVec, etc.) are defined as empty vectors.
As far as I understand, you are trying to add elements to vectors. Because I don't have a reproducible example, I will use my own. Here, add_to_vector adds elements to a and b:
a <- c(1,2)
b <- c("a","b")
add_to_vector <- function(vec, to_add){
vec <- c(vec, to_add)
return(vec)
}
a <- add_to_vector(a, 3)
b <- add_to_vector(b, "c")
Hopefully, this gives you an idea of how R works.
Output
> a
[1] 1 2 3
> b
[1] "a" "b" "c"

save from the IF function the variable

I have functions and would like to save from the IF function the variable "sick [i]" to take advantage of it
for(i in 1:licznik){
print_func <- function(a, b)
{
if(a > b)
{
print('wspolczynnik jest wiekszy' )
print(sick[i])
}
}
print_func(a[i], b[i])
}
How to do it
?
Try this from R for Data Science.
out <- vector("list", length(licznik))
for (i in seq_along(licznik)) {
if(a[[i]] > b[[i]]) {
print('wspolczynnik jest wiekszy')
print(sick[[i]])
out[[i]] <- sick[[i]]
}
}
str(unlist(out))

Data table filtering does not work

I'm trying to create a data table and do some calculations about it for my assignment. However, when I create and manipulate data, for some reason filtering does not work. For instance if I filter column 's' for the value 0.7, no solution. For the value 0.9, it works as it should. It's weird.
Thanks for any help.
library(data.table)
p<-as.data.table(cbind(0:6, c(0,0.15,0.33,0.37,0.40,0.42,0.43)))
states<-seq(from=0, to=30,by=0.1)
actions<-seq(from=0,to=6)
actions<-as.data.table(actions)
actions[,hunt_share:=numeric()]
for(i in 1:7)
{
if(i==1)
actions[i]$hunt_share<-0
else
actions[i]$hunt_share<-floor(164/(i-1)*10)/10
}
u_star<-data.table(t=integer(),s=numeric(),a=integer(),value=numeric())
r<-data.table(t=integer(),s_bar=numeric(),s=numeric(),a=integer(),value=numeric())
trans_prob<-data.table(t=integer(),s_bar=numeric(),s=numeric(),a=integer(),value=numeric())
str(trans_prob)
####### transitional probabilities
horizon<-5
for(time in 1:horizon)
{
for(i in states)
{
for(a in actions$actions)
{
if((a==0))
{
tmp<-data.table(t(c(time,max(i-6,0),i,a,1)))
colnames(tmp)<-colnames(trans_prob)
trans_prob<-rbind(trans_prob,tmp,fill=T)
}
if((a>0)&(i>=0.5))
{
tmp<-data.table(t(c(time,min(30,i+actions[actions==a]$hunt_share-6.5),i,a,p[V1==a]$V2)))
colnames(tmp)<-colnames(trans_prob)
trans_prob<-rbind(trans_prob,tmp,fill=T)
tmp<-data.table(t(c(time,max(i-6.5,0),i,a,1-p[V1==a]$V2)))
colnames(tmp)<-colnames(trans_prob)
trans_prob<-rbind(trans_prob,tmp,fill=T)
}
}
}
print(time)
}
####### transitional probabilities
##Bug?
trans_prob[s==0.9]
trans_prob[s==0.7]
##Bug?

Resources