Add textual property to nodes in igraph with R - r

I have some issue while adding properties of nodes in igraph working with R. I made a text list named journal.txt and I want to give the nodes of my graph a property. With other textual or numeric lists, I had absolutely no issues, but with this one I have.
with this I read the txt file, read just the first column, although there is just one, read as character, although i tried also without and it doesn't work
journalList = read.csv("c:/temp/biblioCoupling/journals.txt", header=FALSE)
journalLR = (journalList[1:303,1])
journalLR = as.character(journalLR)
V(g)$journalName = journalLR
then when I save the file,
write.graph(gr,"filename.gml",format=c("gml"), creator="Claudio Biscaro")
I see all other properties I added to nodes, but not this one!!!
could it be because some entry in journalLR is more than 15 character long?
I have absolutely no idea why I can't do that

Your code is not reproducible, it is impossible to tell for sure, but I guess that V(g)$journalName is a complex attribute, i.e. it is not a vector of values, but a list of values.
To check, you can do str(g) and then look at the code letter after the journalName attribute. If it is x, then it is complex, if it is c, then it is character.
If this is the problem and you don't really need a list, then the workaround is to do
g <- remove.vertex.attribute(g, "journalName")
V(g)$journalName <- journalName

solved by adding one at a time. That was weird. after a long time trying!
for (i in 1:length(journalLR))
{
V(g)[i]$journalName = journalLR[i]
}
probably it is not a formally good solution, but it works!

Related

Transfer variable type to another variable in R

I'd like to transfer the variable type from one variable to the other, where the variables are vectors.
for example x is numeric, y is character but I want it to be the same as x, how to do this? Same if x is integer etc.
pseudo code:
y <- set_variable_type(y,get_variable_type(x))
This has the potential for some errors to crop up. Like #r2evans mentioned converting some characters to numerics doesn't mesh well. It really depends on what the scope is for this code. If you know that type x will always be within certain confines, you will probably be fine with their suggestion class(y)<-class(x), but if you don't know what x will always be, you could end up trying to convert a dataframe into a int, which will throw an error. I would suggest approaching your problem a different way. With more information we may be able to help you find a more comprehensive solution to the problem. (I still can't comment yet, or this would be a comment).

In R, looking for a more detailed str() showing full names or a tree

I want to change parts of a ggplot2 object made by a function and returned as a result, to remove the Y-axis label. No, the function does not allow that to be specified in the first place so I want to change it after the fact.
str(theObject) ## shows the nested structure with parts shortened to ".." and I want to be able to type something like:
theObject$A$B$C$myLabel <- ""
So how can I either make an str -like listing with full paths like that or perhaps draw a tree structure showing the inner working of the object?
Yes, I can figure things out using names(theObject) and finding which branch leads to what I am looking for, then switching to that branch and repeating but it looks like there could be a better automated way to find a leaf node such as:
leaf_str(obj=theObject, leaf="myLabel")
might return zero or more lines like:
theObject$A$B$C$myLabel
theObject$A$X$Y$Z$myLabel
Or, the entire structure could be put out as a series of such lines.
I have searched and found nothing quite like this. I can see lots of uses especially in teaching what an object is. Yes, S4 objects might also use # as well as $.
The
tree
function in the xfun package may be useful.
See here for more details
https://yihui.org/xfun/

Convert characters or symbols to existing variables in R

I'm using R to compute the best fit of a sequence of initializations, and I named them Initialization1, Initialization2, etc.. I compared the best fit with the largest result_probs value. And I want to use the one, say Initialization1, with the best property I want again.
best_fit <- paste("Initialization", which.max(results_probObs), sep = "")
best_estimated <- somefunction(best_fit, string1)
However, best_fit here is a character and can't be used as the existing Initialization1 (which is a list). I've tried as.name() too. It gave me a symbol and couldn't be used as a list as well.
Thank you very much for helping.

R: Want to do a dictionary check and remove unwanted space in between where removing space will make it a proper word

I am using R for text mining and have data that have been concatenated from different text columns. There are cases where words have been split by a space like"functi oning". I want to detect all such cases and remove space in between by doing dictionary check. I know splitWords function in aspell, I want a function exactly opposite of what this does.
Here is an approach, based on some code I found, but you need to provide some example text and even just pseudo code to help others respond.
First create an object that has a huge set of words spelled correctly. Then you compare your vector of words to that set with adist and an argument set to a single difference -- ideally, the internal spaces you would like to remove. I doubt that this will solve everything, but it may help.
sorted_words <- comments(sort(table(strsplit(tolower(paste(readLines("http://www.norvig.com/big.txt"), collapse = " ")), "[^a-z]+")), decreasing = TRUE))
correct <- function(*your vector*) { c(sorted_words[adist(*your vector*, sorted_words) <= min(adist(word, sorted_words), 2)], word)[1] }
Then use the correct function.

Ordered Map / Hash Table in R

While working with lists i've noticed an issue that i didn't expect.
result5 <- vector("list",length(queryResults[[1]]))
for(i in 1:length(queryResults[[1]])){
id <- queryResults[[1]][i]
result5[[id]] <-getPrices(id)
}
The problem is that after this code runs instead of the result staying the same size (w/e queryResults[[1]] is) it goes up to the last index creating a bunch of null entries in the middle.
result5 current stores a number of int,double lists so it looks like :
result5[[index(int)]][[row]][col]
While on it's own it's not too problematic I would rather avoid that simply for easier size calculations later on.
For clarification, id is an integer. And in the given case for loop offers same performance, but greater convenience than the apply functions.
After some testing seems like the easiest way of doing it is :
Using a hash package to convert it using a hash using :
result6 <- hash(queryResults[[1]],lapply(queryResults[[1]],getPrices))
And if it needs to get accessed calling
result6[[toString(id)]]
With the difference in performance being marginal, albeit it's still fairly annoying having to include toString in your code.
It's not clear exactly what your question is, but judging by the structure of the loop, you probably want
result5[[i]] <- getPrices(id)
rather than result5[[id]] <- getPrices(id).

Resources