r

Create formula call from character string

我只是一个虾纸丫 提交于 2021-02-10 20:15:38
问题 I use a best subset selection package to determine the best independent variables from which to build my model (I do have a specific reason for doing this instead of using the best subset object directly). I want to programmatically extract the feature names and use the resulting string to build my model formula. The result would be something like this: x <- "x1 + x2 + x3" y <- "Surv(time, event)" Because I'm building a coxph model, the formula is as follows: coxph(Surv(time, event) ~ x1 + x2

R how to vectorize a function with multiple if else conditions

梦想与她 提交于 2021-02-10 20:15:08
问题 Hi I am new to vectorizing functions in R. I have a code similar the following. library(truncnorm) library(microbenchmark) num_obs=10000 Observation=seq(1,num_obs) Obs_Type=sample(1:4, num_obs, replace=T) Upper_bound = runif(num_obs,0,1) Lower_bound=runif(num_obs,2,4) mean = runif(num_obs,10,15) df1= data.frame(Observation,Obs_Type,Upper_bound,Lower_bound,mean) df1$draw_value = 0 Trial_func=function(df1){ for (i in 1:nrow(df1)){ if (df1[i,"Obs_Type"] ==1){ #If Type == 1; then a=-Inf, b =

Creating a scatter plot using ggvis in a shiny app

最后都变了- 提交于 2021-02-10 20:11:45
问题 I am trying to create a simple app that does the following: Import a csv file as a reactive (this has to be a reactive) Print the contents of csv as data table in one tabPanel Create an interactive scatter plot using ggvis in another tabPanel However I am unable to create the plot- the tabPanel where plot is supposed to appear is blank. There is no error or warning messages in the console whatsoever. Not sure what is wrong with the code. Here is the code: # Define UI for application ui <-

Inserting NA in blank values from web scraping

无人久伴 提交于 2021-02-10 20:11:25
问题 I am working on scraping some data into a data frame, and am getting some empty fields, where I would instead prefer to have NA. I have tried na.strings, but am either placing it in the wrong place or it just isn't working, and I tried to gsub anything that was whitespace from beginning of line to end, but that didn't work. htmlpage <- read_html("http://www.gourmetsleuth.com/features/wine-cheese-pairing-guide") sugPairings <- html_nodes(htmlpage, ".meta-wrapper") suggestions <- html_text

Inserting NA in blank values from web scraping

怎甘沉沦 提交于 2021-02-10 20:10:36
问题 I am working on scraping some data into a data frame, and am getting some empty fields, where I would instead prefer to have NA. I have tried na.strings, but am either placing it in the wrong place or it just isn't working, and I tried to gsub anything that was whitespace from beginning of line to end, but that didn't work. htmlpage <- read_html("http://www.gourmetsleuth.com/features/wine-cheese-pairing-guide") sugPairings <- html_nodes(htmlpage, ".meta-wrapper") suggestions <- html_text

R how to vectorize a function with multiple if else conditions

自作多情 提交于 2021-02-10 20:08:33
问题 Hi I am new to vectorizing functions in R. I have a code similar the following. library(truncnorm) library(microbenchmark) num_obs=10000 Observation=seq(1,num_obs) Obs_Type=sample(1:4, num_obs, replace=T) Upper_bound = runif(num_obs,0,1) Lower_bound=runif(num_obs,2,4) mean = runif(num_obs,10,15) df1= data.frame(Observation,Obs_Type,Upper_bound,Lower_bound,mean) df1$draw_value = 0 Trial_func=function(df1){ for (i in 1:nrow(df1)){ if (df1[i,"Obs_Type"] ==1){ #If Type == 1; then a=-Inf, b =

Assign colors to communities in igraph

瘦欲@ 提交于 2021-02-10 20:08:16
问题 I am using the fastgreedy.community detection algorithm in igraph to produce communities in R. The code returns 12 communities, however they are difficult to indentify when plotting because it returns a plot with a limited number of colours. How can I plot this graph with tweleve distinct colours? l2 <- layout.fruchterman.reingold(largest.component) ebc.g05 <- fastgreedy.community(largest.component) plot(largest.component, layout=l2, vertex.color=membership(ebc.g05), vertex.size=2, vertex

Expanding R Matrix on Date

扶醉桌前 提交于 2021-02-10 20:01:45
问题 I have the following R matrix: Date MyVal 2016 1 2017 2 2018 3 .... 2026 10 What I want to do is "blow it up" so that it goes like this (where monthly values are linearly interpolated): Date MyVal 01/01/2016 1 02/01/2016 .. .... 01/01/2017 2 .... 01/01/2026 10 I realize I can easily generate the sequence using: DateVec <- seq(as.Date(paste(minYear,"/01/01", sep = "")), as.Date(paste(maxYear, "/01/01", sep = "")), by = "month") And I can use that to make a large matrix and then fill things in

Loop through dataset to calculate diveristy

心不动则不痛 提交于 2021-02-10 20:01:35
问题 I have a dataset like so: set.seed(1345) df<-data.frame(month= c(rep(1,10), rep(2, 10), rep(3, 10)), species=sample(LETTERS[1:10], 30, replace= TRUE)) I would like to loop through each month and calculate species diversity. I am aware of functions like diversity in library("vegan") , and know solutions to my question using that route (code provided below), but as an exercise for myself with loops I am trying to create a for loop or function that shows the specific calculations for Shannons

Assign colors to communities in igraph

微笑、不失礼 提交于 2021-02-10 20:01:08
问题 I am using the fastgreedy.community detection algorithm in igraph to produce communities in R. The code returns 12 communities, however they are difficult to indentify when plotting because it returns a plot with a limited number of colours. How can I plot this graph with tweleve distinct colours? l2 <- layout.fruchterman.reingold(largest.component) ebc.g05 <- fastgreedy.community(largest.component) plot(largest.component, layout=l2, vertex.color=membership(ebc.g05), vertex.size=2, vertex