install.packages("rjson")
library("rjson")
result <- fromJSON(file="provdata.json.txt")
for (i in seq(1,5,1))
{
term <- result$data[i]
print(term)
}
I was wondering what should I put to only print 13.6 and 4.15. Thank you!
I can't check for sure the structure of the json list, as you don't provide a reproducible example (please check https://stackoverflow.com/help/how-to-ask), but I think your problem is that you're not subsetting the list deep enough:
instead of the
term <- result$data[i]
print(term)
bit, try with
print(result$data[[1]][[1]][2])
or something similar. Alternatively, please use dput on a subset of your result variable, so we can help better.
Related
I have been learning how to code in R recently, so I'm not familiarized with apply at all. As far as I know loops are not so efficient in R, so I'm trying to use apply function but I'm not getting any results.
This is my loop:
encoder_output <- function(sequence, vocabulary){
auxlist <- list()
for (i in sequence) {
encoded <- to_categorical(i, num_classes=vocabulary)
auxlist <- append(auxlist, encoded)
}
arrOutput <- array(unlist(auxlist),dim =c(nrow(sequence),ncol(sequence),vocabulary))
return(arrOutput)
}
And here is my apply:
encode_output <- function(sequence, vocabulary){
auxlist <- list()
apply(sequence, 1,function(x){
encoded <- to_categorical(x, num_classes=vocabulary)
auxlist <- append(auxlist, encoded)
})
array <- array(unlist(auxlist), dim= c(nrow(sequence),ncol(sequence),vocabulary) )
return(array)
}
But in my apply function, I'm getting an error in unlist, because it says that auxlist is empty.
I don't know what I'm doing wrong. Btw, sequence is a 2D matrix. I believe that this code is enough to solve my question, but if necessary I will update it with more code.
Thanks guys!
PS: I'm using keras library to user to_categorical.
Ah. This is the classic coding environment issue. The return function will only give you a result within the code, but not show up in your environment. So try this:
assign(New_array_Name,array = df,.GlobalEnv)
Another way to do it is to have it as an output to assign it to another 'external' variable. In this case, remove return(arrOutput), replace it with arrOutput. And, in the console or wherever you run your code, use the following line.
variable <- encoder_output(...)
I'm new here and new to R and I think I have a simple question but don't know how to name it so I can't find any help by searching the web.
I have a data set and want to form a new Data set with several variables from the first one.
The working code looks like this:
em.table2 <- data.frame(em.table$item1,em.table$item2,...[here are some more]...,em.table$item22)
In order to keep it more simple, I want to get rid of the "em.table$"-construction in front of every variable... unfortunately i don't know the function to do so...
I tried it like this, but it didn't work (and is a pretty embarrasing try i guess):
em.table2 <- data.frame(em.table$(item1,item2,item3,item4))
Anyone here to help? Thanks a lot!
Instead of the $ operator, try the following:
em.table2 <- em.table[,c("item1","item2","item3","item4")]
Try with
em.table2 <- with(em.table, data.frame(item1, item2, item3, item4))
But if you just want to subset the data, there are better solutions.
I'm very new to R and am working on a text mining project. I have all the analysis working however when I convert the Term-Document Matrix back to a data frame it fills the console with the content..
The code I'm using for this is:
TDM.frame <- data.frame(inspect(Words.TDM))
The frame has 9k objects in it so I won't paste that here too but you can imagine what the console looks like when it dumps the whole content out ^^
I've tried using invisible() but that doesn't change anything. I hope someone can tell me what I'm doing wrong, or offer a solution!
Thanks!
This is what inspect does (at least in the case where it is given a TDM):
> tm:::inspect.TermDocumentMatrix
function (x)
{
print(x)
cat("\n")
print(as.matrix(x))
}
<environment: namespace:tm>
So you want the object that is returned which is just as.matrix(tdm) and you do not want the printing side-effect. So you should just do this instead:
TDM.frame <- data.frame(as.matrix(Words.TDM))
Is the inspect() within data.frame() really necessary? Can you perhaps just convert the TDM to a matrix, since it seems this is what you are trying to achieve? If necessary, you can then convert the matrix to a data frame.
as.matrix(Words.TDM)
Try
TDM.frame <- data.frame(inspect(Words.TDM))
head(TDM.frame)
Or, you can use dplyr
library(dplyr)
TDM.frame <- tbl_df(TDM.frame)
Thanks in advance.
Wondering if there is a way to call a ui element via string in the server.r file.
Example:
instead of ...
ui.Color <- input$color
do something like ...
ui.Color <- input[,"color"]
...kind of like you could do with a data frame. Issue is class(input) yields reactiveValues so I'm not quite sure how to subset it to get a particular value.
Thanks all!
Got it!
input[["color"]]
Hope this helps others with the same question!
I would like to print a nice formatted table to a graphics device. I am aware there is textplot(), however it seems not to support the tabular() function from the tables package, which I use to assemble my table.
Is there a workaround, maybe another pair of functions that can help me here?
EDIT:
Here is an example of what I am trying to do:
dat <- data.frame(
id=paste("id", 1:10),
loc=sample(c("north", "south"), 10, replace=TRUE),
val1=rnorm(10),
val2=rnorm(10)
)
tab <- tabular(id + 1 ~ (val1 + val2)*loc*sum, data=dat)
#textplot(tab) # won't do it
(The call to tabular will become more complex, I am currently learning it step by step...)
Any hint appreciated! I am now thinking of using text with a monospaced fonts, but maybe there is something better?
EDIT2:
Here is the accepted solution:
textplot(capture.output(tab))
You could use capture.output with the printed form of the tabular output then pass the result to textplot. Or you could convert the result of tabular to a matrix and pass that to the addtable2plot function in the plotrix package.
I've been working on an update of gridExtra::grid.table that would support basic features of tabular(). You can try it:
# requires gtable
library(devtools)
source_gist(2013903)
The only graphics device that I know that would possibly accept LaTeX input is the tikzDevice::tikz. You might want to include the code your are sending to tables::tabular and we could see if the output is handled naturally.