Get help file description in R as an object [duplicate] - r

This question already has answers here:
R help page as object
(2 answers)
Closed 4 years ago.
I'm trying to build a shiny app that manipulates some base R datasets, like datasets::airmiles. I'd like to display somewhere within that app the description of this dataset. For instance, ?datasets::airmiles yields -
Is there any way I can extract the description string? e.g. in this case, it would be the string 'The revenue passenger miles flown by commercial airlines in the United States for each year from 1937 to 1960.'
I have tried -
x <- help('airmiles')
str(x)
# 'help_files_with_topic' chr "C:/Program Files/R/R-3.5.1/library/datasets/help/airmiles"
# - attr(*, "call")= language help(topic = "airmiles")
# - attr(*, "topic")= chr "airmiles"
# - attr(*, "tried_all_packages")= logi FALSE
# - attr(*, "type")= chr "html"
But x does not contain the description of airmiles.

Seems to work for what you want but I'd take out the semi-colons:
R help page as object
Can be accessed by var$description.

Related

How to get the labels of items in an R list [duplicate]

This question already has an answer here:
Accessing list names
(1 answer)
Closed 7 years ago.
I have an object of type "Large list" and I want to get a list of the labels that follow the dollar signs below. How do I do that?
str(example_list)
List of 360
$ b32ad9c4fcbdd1f812 : chr [1:2201] "sd" "ssd" "dgaAL" "dE6" ...
$ 6ba9eb1aa59226b8 : chr [1:2320] "83r" "iTY" "dkeM" "4Kl" ...
$ e1680cf14ebc88bbd521 : chr [1:2687] "62V" "dAe" "ddv" "dAL" ...
You would use the names function because each item in the larger list is itself a list with its own name.
So with one item:
names(example_list[1])
we get
[1] "b32ad9c4fcbdd1f812"
Then with the list of lists
names(example_list)
we get
[1] "b32ad9c4fcbdd1f812", "6ba9eb1aa59226b8", "e1680cf14ebc88bbd521"
from an answer to this question: extract names of objects from list

text searching in R

I am trying to make a query to use in a R package named RISmed, which will search and downloaded relevant journal article information from pubmed database. I want to search two words always together, for example :
query= "gene sequencing"
search<-EUtilsSummary(query,type="esearch",db = "pubmed",mindate=2014, maxdate=2014, retmax=20)
If I use, above command, it will search gene and sequencing separately, then both gene and sequencing,that means if in whole text gene and sequencing exists, my command captures them but I want to search in such a way, that it will consider "Gene sequencing", two words always together. How can I write that query? Would anyone please help me?
Thanks in advance !
I would try this:
query <- '"gene sequencing"[Title/Abstract]'
The pubmed search engine does accept quoted strings and you just need to know how to preserve them within R. Using surrounding single quotes is one method. Using back-slashed quotes would be another. Notice that the returned value from my experiment with your code shows that escape-backslashing is how the implemeters of that package do it:
> str(search)
Formal class 'EUtilsSummary' [package "RISmed"] with 6 slots
..# db : chr "pubmed"
..# count : num 542
..# retmax : num 20
..# retstart : num 0
..# PMID : chr [1:20] "25548628" "25543043" "25542841" "25540641" ...
..# querytranslation: chr "\"gene sequencing\"[Title/Abstract] AND 2014[EDAT] : 2014[EDAT]"

Hidden Markov Model in R - Predict the next observation with RHmm

This is my first post on StackOverflow and I could use a little help... Please forgive me if I am not following the correct posting protocols.
There is another example in the StackOverflow for which I am heavily basing my work off of but I cant quite figure out how to adapt the code. Most importantly, I am looking at the solution to the question provided.
Here is the link:
Getting the next observation from a HMM gaussian mixture distribution
Some background:
RHmm - version 2.1.0 downloaded from R Forge.
RStudio - 0.98.953
R - 3.0.2 32 bit
I am trying to figure out the following issues with my code:
How do I amend the solution from the link above (prediction of the next observation) to work with my Baum-Welch model?
Ex. hm_model <- HMMFit(obs=TWII_Train, nStates=5)
The R / RStudio session aborts when I run the Baum-Welch version of the hm_model <- HMMFit(obs=TWII_Train, dis="MIXTURE", nStates=5, nMixt=4). Can you recreate the error and propose a workaround?
Here is my R code:
library(quantmod)
library(RHmm)
getSymbols("^TWII")
TWII_Subset <- window(TWII, start=as.Date("2012-01-01"), end = as.Date("2013-04-01"))
TWII_Train <- cbind(TWII_Subset$TWII.Close - TWII_Subset$TWII.Open,
TWII_Subset$TWII.Volume)
hm_model <- HMMFit(obs=TWII_Train, nStates=5)
VitPath <- viterbi(hm_model, TWII_Train)
I'm not a user of this package and this is not really an answer, but a comment would obscure some of the structures. It appears that the "proportion" value of your model is missing (so the structures are different. The "mean" value looks like this:
$ mean :List of 5
..$ : num [1:2] 6.72 3.34e+06
..$ : num [1:2] -12.4 2420174.5
..$ : num [1:2] -2.4 1832546.5
..$ : num [1:2] -10.4 1432636.1
..$ : num [1:2] 5.02 1.96e+06
I also suspect that you should be using 2 and 5 rather than 4 and 5 for m and n. Look at the rest of the model with:
str(hm_model)

Exporting data from UScensus2010

I need to extract Census 2010 data for CDPs (nationwide) and figured it would be simplest to use the UScensus2010 package to extract the key tables I need, one state at a time, and then export the resulting data. Towards this end I ran the following code
library(UScensus2010)
install.cdp("osx")
demographics(dem="P0030001", state="oh",level="cdp")
p3.race <- demographics(dem=c("P0010001", "P0030001", "P0030002", "P0030003", "P0030004", "P0030005", "P0030006"), state="oh",level="cdp")
Note that for p3.race we have
..$ : chr [1:1204] "Hamler village" "Florida village" "Deshler village" "New Bavaria village" ...
..$ : chr [1:7] "P0010001" "P0030001" "P0030002" "P0030003" ...
So far so good. Now, what would be the best way to export p3.race (as *.csv *.dta, or any other format) such that the CDP names show up as column A and P001001:P0030006 show up as Columns B:H?

Regarding creating a data set in accordance with a given data format

I am learning to use topicmodels package and R as well, and explored one of its example data set by using
str(testdata)
'data.frame': 3104 obs. of 5 variables:
$ Article_ID: int 41246 41257 41268 41279 41290 41302 41314 41333 41344 41355 ...
$ Date : chr "1-Jan-96" "2-Jan-96" "3-Jan-96" "4-Jan-96" ...
$ Title : chr "Nation's Smaller Jails Struggle To Cope With Surge in Inmates" "FEDERAL IMPASSE SADDLING STATES WITH INDECISION" "Long, Costly Prelude Does Little To Alter Plot of Presidential Race" "Top Leader of the Bosnian Serbs Now Under Attack From Within" ...
$ Subject : chr "Jails overwhelmed with hardened criminals" "Federal budget impasse affect on states" "Contenders for 1996 Presedential elections" "Bosnian Serb leader criticized from within" ...
$ Topic.Code: int 12 20 20 19 1 19 1 1 20 15 ...
If I want to create a data set according to the above format in R, how to do that?
test.data is a data.frame, one of the few fundamental R objects. You should probably start here: http://cran.r-project.org/doc/manuals/R-intro.pdf.
Some functions for creating data.frames are data.frame, read.table, read.csv. For each of these you can access their documentation by typing ?data.frame for example. Good luck.

Resources