FAQ Database Discussion Community


Replace -inf, NaN and NA values with zero in a dataset in R

r,time-series,nan,zoo
I am trying to run some trading strategies in R. I have downloaded some stock prices and calculated returns. The new return dataset has a number of -inf, NaN, and NA values. I am reproducing a row of the dataset (log_ret). Its a zoo dataset. library(zoo) log_ret <- structure( c(0.234,-0.012,-Inf,NaN,0.454,Inf),...

Count number of rows meeting criteria in another table - R PRogramming

r
I have two tables, one with property listings and another one with contacts made for a property (i.e. is someone is interested in the property they will "contact" the owner). Sample "listings" table below: listings <- data.frame(id = c("6174", "2175", "9176", "4176", "9177"), city = c("A", "B", "B", "B" ,"A"),...

Store every value in a sequence except some values

r
If I do the following to a string of letters: x <- 'broke' y <- nchar(x) z <- sequence(y) How do I store every value of the z that isn't the first, last, or middle values of the sequence. In this example if z is 1 2 3 4 5...

Translating Stata to R: collapse

r,data.table,stata,code-translation
Just came across a .do file that I need to translate into R because I don't have a Stata license; my Stata is rusty, so can someone confirm that the code is doing what I think it is? Here's the Stata code: collapse (min) MinPctCollected = PctCollected /// (mean) AvgPctCollected...

how to read a string as a complex number?

r
I have a string which has a complex format, how can I use complex() to treat it as a complex number? For example: myStr="0.76+0.41j" now I want to do sth like: myStr_complex=complex(myStr) # my question is how should I do this part? Eventually Im(myStr_complex) should print 0.41 ...

How to repeat this statement in R probably using apply()

r,loops
It might seem a silly question but how to repeat this line for 152 times and I would not like to use a for loop,since later it will not be efficient with larger data sets: reviews = as.vector(t(mydata)[,1]) mydata is a row in a data.frame and reviews is an array...

How can I generate all the possible combinations of a vector

r,vector
I have a vector, say A,B,C,D,E and I am interested in generating all the possible combination of the vector elements. The desired output is given below. B-A,C-A,D-A,E-A,C-B,D-B,E-B,D-C,E-C,E-D...

ggplot equivalent for matplot

r,ggplot2
Is there an equivalent in ggplot2 to plot this dataset? I use matplot, and read that qplot could be used, but it really does not work. ggplot/matplot data<-rbind(c(6,16,25), c(1,4,7), c(NA, 1,2), c(NA, NA, 1)) as.data.frame(data) matplot(data, log="y",type='b', pch=1) ...

Sequentially create date for each record by ID in R datatable

r
I have a datatable with ID and origination date, each unique ID represent a row. I have to use the variable 'COUNT' (which is essentially the interval between orig_date and close_date in months) and sequentially replicate ORIG_DATE into DATE field as shown. The code I tried is taking only the...

Apply a list of n *expressions* to each row of a dataframe?

r,apply,lapply,mapply
In short, I have a list of expressions that I want to apply to each row of a dataframe. This is very similar to this question, but there is a subtle difference in that I do not have a list of functions, but have a list of expressions. Here's what...

Creating indicator column based on conditional categorizing of rows in R

r
I have a dataset like this: user.id user_type 3 old 3 old 4 new 5 new 5 new 6 old I want to create new column that would indicate the first time user_type is 'new' for each user_id. Output should look like this: user.id user_type indicator 3 old 2 3...

Twitter: Get followers from multiple users at once

r,twitter
I am working on a project where I need to find the reach of some social events. I want to know how many people who were exposed to comments on a festival called Tinderbox in Denmark. What I do is to get the statusses on Twitter including the word "tinderbox"...

Keep the second occurrence in a column in R

r,conditional,subset,find-occurrences
I have quite a simple dataset: ID Value Time 1 censored 1 1 censored 2 1 uncensored 3 1 uncensored 4 1 censored 5 1 censored 6 2 censored 1 2 uncensored 2 2 uncensored 3 2 uncensored 4 2 censored 5 I want to keep the first uncensored occurrence,...

Problems with apply R

r,svm,apply
I Have a problem with using the apply function in R. I made the following function: TrainSupportVectorMachines <- function(trainingData,kernel,G,C){ ####train het model fit<-svm(Device~.,data=trainingData,kernel=kernel,probability=TRUE, gamma =G, costs=C) return(fit); } I want to train the model with different values of Cost(c). Therefore, I tried the following commend: cst = matrix(2^(-4:-2),ncol=3) kernl =...

Applying a function to each quantile of an R dataframe

r,data.frame,quantile
I have an R dataframe and I want to apply an estimation function for each of its quantiles. Here's an example with lm(): df <- data.frame(Y = sample(100), X1 = sample(100), X2 = sample(100)) estFun <- function(df){lm(Y ~ X1 + X2, data = df)} If I split that in two...

subset data.table keeping only elements greater than certain value applied to all columns

r,data.table,subset
I would like to subset news (below) to create news2 (further below) which will only include the rows/columns where the abs(value) in each element of news > 0.01. Below is the code that I have tried: gr <- data.frame(which(abs(news[, 1:ncol(news), with = FALSE]) > 0.01, arr.ind = TRUE)) news2a <-...

Highlighting specific ranges on a Graph in R

r,graph,highlight
library(season) plot(CVD$yrmon, CVD$cvd, type = 'o',pch = 19,ylab = 'Number of CVD deaths per month',xlab = 'Time') if i wanted to highlight a region of the graph based on x values say from 1994-1998 how do i do this? Any thought would be appreciated Thanks....

agrep working with del, ins arguments

r,arguments,string-matching,agrep
How can "abteam" with "ab" be matched using this code? agrep("abteam",c("acb","abd","ab"),value=T,ignore.case = TRUE,max = list(del = 10, ins = 10, sub = 10)) The result is character(0), though I specified del=10, ins=10. What is the problem? How does agrep work?...

R using ggplot2: “Error in value == ”primary“ : comparison is not allowed for expressions”

r,ggplot2,facet
I am attempting to change the labels in the handy facet plot available in R with ggplot using a function that I created, facet_labeller: #This function will map a new name to the category labels facet_labeller <- function(var, value){ value <- as.character(value) # Creates labels with superscript for 1st and...

Return Column Names when True in R

r
I am using R for a project and I have a data frame in in the following format: A B C 1 1 0 0 2 0 1 1 I want to return a data frame that gives the Column Name when the value is 1. i.e. Impair1 Impair2 1...

copy a list of data.tables

r,data.table
I have the following situation: 1) a list of data tables 2) For testing purposes I deliberately want to (deeply) copy the whole list including the data tables 3) I want to take some element from the copied list and add a new column. Here is the code: library(data.table) x...

Grow a ffdf data frame on disk gradually

r,ff,ffbase
From documentation of save.ffdf: Using ‘save.ffdf’ automagically sets the ‘finalizer’s of the ‘ff’ vectors to ‘"close"’. This means that the data will be preserved on disk when the object is removed or the R sessions is closed. Data can be deleted either using ‘delete’ or by removing the directory where...

Count of data by Sqldf

r,sqldf
My data looks like: ID category 101 A 101 B 101 C 102 A 103 B 103 C I would like the result like: ID category Count 101 A 3 101 B 3 101 C 3 102 A 1 103 B 2 103 C 2 I have tried something like:...

can match() have a range included in R?

r,match
I am trying to use match() in R to find any matching values within a certain interval. For example: v <- c(2.2, 2.4, 4.3, 1.3, 4.5, 6.8, 0.9) match(2.4, v) gives me all the locations where 2.4 occurs in v, but what if I wanted to give a range for...

Using R to Assign Treatments to Groups

r
We have seven exposures and 24 groups. We would like to randomly assign five of the seven exposures to groups while also ensuring that we end up with a consistent count for each exposure, meaning that each exposure ends up being exposed about the same number of times. I have...

R k mean and heircal clustering takes forever time to finish

r
I have a data set (after normalising and preprocessing) contains a data frame that has 5 columns and 133763 rows. I am trying to apply k means algorithm, and herical algorithm in order to do the clustering. However, my problem is that R studio keeps trying to do the calculation,...

Reshape column values to column names

r,reshape
I've got a dataset with the following structure: df <- data.frame(mult=c(1,2,3,4),red=c(1,0.9,0.8,0.7), result=c('value1','value2','value3','value4')) that I'd like to display in a 3-D plot (x axis: mult, y axis: red, and the x-y points would be 'result') or multiple 2-D plots. Obviously the real DF has a lot more rows and combinations of...

>= not working, R [duplicate]

r,if-statement,double,logic
This question already has an answer here: Why are these numbers not equal? 3 answers I am running the code below. The function Ax returtns the value 1. But the ifelse statement does not recognize that Ax==1. I've tried making the output of the function double-precision and all numeric...

How can I use values and charts in a knitr / LaTeX document before R has calculated them?

r,latex,knitr
A knitr document contains sections of R and LaTeX. My boss wants to read the summary (in LaTeX), but doesn't want to read the R. However the R should be available in an appendix, so the code can be checked if needs be (see below). How to I make values...

Sequence index plots in ggplot2 using geom_tile( )

r,ggplot2,traminer
I'm trying to use ggplot to create sequence plots, for the sake of keeping the same visual style within my paper using sequence analysis. I do: library(ggplot2) library(TraMineR) library(dplyr) library(tidyr) data(mvad) mvad_seq<-seqdef(mvad,15:length(mvad)) mvad_trate<-seqsubm(mvad_seq,method="TRATE") mvad_dist<-seqdist(mvad_seq,method="OM",sm=mvad_trate) cluster<-cutree(hclust(d=as.dist(mvad_dist),method="ward.D2"),k=6) mvad$cluster<-cluster...

Rbind in variable row size not giving NA's

r,rbind
The initial data frame mergedDf is PROD_CODE 1 PRD0900033,PRD0900135,PRD0900220,PRD0900709 2 PRD0900097,PRD0900550 3 PRD0900121 4 PRD0900353 5 PRD0900547,PRD0900614 After calling mergedDf<-data.frame(do.call('rbind', strsplit(as.character(mergedDf$PROD_CODE),',',fixed=TRUE))) Output becomes X1 X2 X3 X4 1 PRD0900033 PRD0900135 PRD0900220 PRD0900709 2 PRD0900097 PRD0900550 PRD0900097 PRD0900550 3 PRD0900121 PRD0900121 PRD0900121 PRD0900121 4 PRD0900353 PRD0900353 PRD0900353 PRD0900353 5 PRD0900547 PRD0900614...

Remove quotes to use result as dataset name

r,string
I've got a vector with a long list of dataset names. E.g myvector<-c('ds1','ds2,'ds3') I'd like to use the names ds1..ds3 to write a file, taking the file name from the vector. Like this: write.csv(dataset[i],file=paste(myvector[i],'.csv',sep='') with dataset being d1...ds3, but without quotes. How can I remove the quotes and refer to...

Find multiple consecutive empty lines

r
I'm trying to chop up a text file into the articles it contains. Usually this is done by identifying a pattern each article begins with. Unfortunately the database I downloaded the articles from doesn't have that. The only pattern I can find is that after each article there are 3...

R: replace values in data frame, in list [closed]

r
I would like to replace each value with proper name for all columns in each dataframe. I tried this: data <- lapply(myfilelist, function(x) { x <- dat_name[x[, ]] return(x) }) but it doesn't work (or it is possible only for one column but not for all of them at once)....

Add a calculated column to data frame based on another data frame

r
I have 2 data frames in R: tvNationalSale: each row is one TV ad placement workingNational: each row is total web sessions by minute I want to add a calculated column to tvNationalSale that contains the sum of sessions in the 5 minutes prior to an ad showing. I am...

R Program Vector, record Column Percent

r,vector,percentage
This is my vector head(sep) I must find percent of all SEP 11 in each row. For instance, in first row, percent of SEP 11 is 100 * ((63 + 124)/ (63 + 124 + 0 + 0)) And would like this stored in newly created 8th column Thanks dput...

Limiting interpolation function to NA values

r,interpolation,zoo,spline
I am trying to use na.spline, part of the zoo package, to replace NA values in some imported speed data with a cubic spline interpolated values. na.spline is modifying the NA values as it is supposed to; however, it is also modifying values that originally equaled 0. ex <- data.frame(speed)...

optimization algorithm for circular data

r,optimization,circular,maximization
Background: I am interested in localizing a sound source from a suite of audio recorders. Each audio array consists of 6 directional microphones spaced evenly every 60 degrees (0, 60, 120, 180, 240, 300 degrees). I am interested in finding the neighboring pair of microphones with the maximum set of...

R: recursive function to give groups of consecutive numbers

r,if-statement,recursion,vector,integer
Given a sorted vector x: x <- c(1,2,4,6,7,10,11,12,15) I am trying to write a small function that will yield a similar sized vector y giving the last consecutive integer in order to group consecutive numbers. In my case it is (defining groups 2, 4, 7, 12 and 15): > y...

How to quickly read a large txt data file (5GB) into R(RStudio) (Centrino 2 P8600, 4Gb RAM)

r,large-data
I have a large data set, one of the files is 5GB. Can someone suggest me how to quickly read it into R (RStudio)? Thanks

Sleep Shiny WebApp to let it refresh… Any alternative?

r,shiny,sleep
I have a WebApp that have some renderUI({})... and some of them depend on the input of another. This makes that, briefly, a red error in the webpage appear when I select some options. Because the if() clause of some renderUI({}) depend on the input of a selectizer. The error...

How to use parameters from data frame in R and loop through time holding them constant

r,nested,time-series,lapply,sapply
I have a function (weisurv) that has 2 parameters - sc and shp. It is a function through time (t). Time is a sequence, i.e. t<-seq(1:100). weisurv<-function(t,sc,shp){ surv<-exp(-(t/sc)^shp) return(surv) } I have a data frame (df) that contains a list of sc and shp values (like 300+ of them). For...

Saving elements of a list as data.frames using R

r,list,save,lapply
How can I save each element of a list in a in a separate .RData file? Consider the following example: # Generating a list containing 3 matrices set.seed(1) mylist=list(M1=matrix(LETTERS[sample(1:26,9)],3),M2=matrix(LETTERS[sample(1:26,9)],3),M3=matrix(LETTERS[sample(1:26,9)],3)) mylist[1:2] # $M1 # [,1] [,2] [,3] # [1,] "G" "U" "W" # [2,] "J" "E" "M" # [3,] "N" "S"...

Histogram-like summary for interval data

r,statistics,histogram
How do I get a histogram-like summary of interval data in R? My MWE data has four intervals. interval range Int1 2-7 Int2 10-14 Int3 12-18 Int4 25-28 I want a histogram-like function which counts how the intervals Int1-Int4 span a range split across fixed-size bins. The function output should...

R — frequencies within a variable for repeating values

r,count,duplicates
I've got a column A, which has several values, some of them repeating. So, example: A = c(5, 9, 6, 5, 5). I need to go through A and count the frequencies of each of the values in A. So, for this example, for the set of 5s in A,...

Linear multivariate regression in R

r
I want to model that a factory takes an input of, say, x tonnes of raw material, which is then processed. In the first step waste materials are removed, and a product P1 is created. For the "rest" of the material, it is processed once again and another product P2...

how to get values from selectInput with shiny

r,shiny
I am playing around with the shiny packages for some hours now, and wanted to make a select input widget that enables me to download a certain data set from the server. So i figured out a way to get me this data frame containing all my IDs for downloading:...

Subtract time in r, forcing unit of results to minutes [duplicate]

r,posix,posixct
This question already has an answer here: Getting consist units from diff command in R 4 answers I successfully subtracted two POSIXct cols of df1 (below). However, since the time differences are >= 1 hour in all rows, R gives the results in hours. I know that this make...

Transforming irregular data into usable format in R

r,data.table,transformation
Suppose I work in a company that provides a number of different services to their customers. I have been given a data report on the services provided that I need to analyse. The report is formatted in a way to be easily read and printed, but not suitable for data...

Using Yahoo! database without quantmod functions

r,loops,yahoo-finance
The problem I am trying to solve is looping a string through R with Yahoo! finance api. This would make a bunch of data frame files, but if I could convert it into xts, that would be awesome. However, the xts part is not as important. library(quantmod) DB <- quantmod:::DDB_Yahoo()...

How to split a text into two meaningful words in R

r,string-split,stemming,text-analysis
I had a text data frame having sentences, and as I wanted the list of separate words in another dataframe I used the "qdap package" function "all_words" Words = all_words(df$problem_note_text, begins.with=NULL , alphabetical = FALSE, apostrophe.remove = TRUE, char.keep = char2space, char2space = "~~") Now have a dataframe which has...

ggplot2 & facet_wrap - eliminate vertical distance between facets

r,ggplot2
I'm working with some data that I want to display as a nxn grid of plots. Edit: To be more clear, there's 21 categories in my data. I want to facet by category, and have those 21 plots in a 5 x 5 square grid (where the orphan is by...

Select / subset spatial data in R

r,dictionary,spatial
I am working on a large data set with spatial data (lat/long). My data set contains some positions that I don´t want in my analysis (it makes the files to heavy to process in ArcMap- many Go of data). This is why I want to subset the relevant data for...

How to plot data points at particular location in a map in R

r,google-maps,ggmap
I have a dataset that looks like this: LOCALITY numbers 1 Airoli 72 2 Andheri East 286 3 Andheri west 208 4 Arya Nagar 5 5 Asalfa 7 6 Bandra East 36 7 Bandra West 72 I want to plot bubbles (bigger the number bigger would be the bubble) inside...

How to set x-axis with decreasing power values in equal sizes

r,plot,ggplot2,cdf
Currently I am doing some cumulative distribution plot using R and I tried to set x-axis with decreasing power values (such as 10000,1000,100,10,1) in equal sizes but I failed: n<-ceiling(max(test)) qplot(1:n, ecdf(test)(1:n), geom="point",xlab="check-ins", ylab="Pr(X>=x)")+ geom_step() +scale_x_reverse(breaks=c(10000,1000,100,10,1)) +scale_shape_manual(values=c(15,19)) It seems that the output has large interval for 10000, then all the...

Remove escaping \n

regex,r
Some data: x <- c("Town\[email protected]\n\n","\[email protected]\n","\[email protected]\n\n","\[email protected]\n","[email protected]") This would work but there is word before \n gsub("\n","",x,fixed=T) This works but if there isn't \n gives NA unlist(lapply(regmatches(x,gregexpr("(?<=\n).*",x,perl=TRUE)), function(x) x[1])) EDIT: Desired result: rep("[email protected]",5) ...

Convert strings of data to “Data” objects in R [duplicate]

r,date,csv
This question already has an answer here: as.Date with dates in format m/d/y in R 2 answers My problem is that the as.Date function does not convert the values in a "date" column of a data frame into Date objects. I have a data.frame nmmaps. Here is a short...

Set a timer in R to execute a program

r,timer
I have a program to execute per 15 seconds, how can I achieve this, the program is as followed: print_test<-function{ cat("hello world") } ...

Split data table by row number in R

r
I have a data table and one of the columns is a bunch of 0's and 1's, just like vec below. vec = c(rep(1, times = 6), rep(0, times = 10), rep(1, times = 11), rep(0, times = 4)) > vec [1] 1 1 1 1 1 1 0 0...

Allow grouping with NA in aggregate function

r,aggregate
Here is dummy data temp.df <- data.frame(count = rep(1,6), x = c(1,1,NA,NA,3,10), y=c("A","A","A","A","B","B")) When I apply aggregate as given below: aggregate(count ~ x + y, data=temp.df, FUN=sum, na.rm=FALSE, na.action=na.pass) I get: x y count 1 1 A 2 2 3 B 1 3 10 B 1 However, I would like...

R readHTMLTable failed to load external entity [duplicate]

xml,r,connection
This question already has an answer here: R Error using readHTMLTable 2 answers When I run the line on my laptop, table500 <- readHTMLTable('http://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[[1]] it gets the data. When I run it on my desktop, I receive the error Error: failed to load external entity "http://en.wikipedia.org/wiki/List_of_S%26P_500_companies". So I'm guessing...

Replace improper commas in CSV file

regex,r,csv
This may have been asked before, but I couldn't find it. I have a list of CSV files (439 or so) where, in a few of the files, someone also used commas in editorial comments. The result is that I can't put the files into a data frame, since the...

Converting column from military time to standard time

r,excel
I'm trying to convert a column showing the time of road traffic accidents from military time to standard time. The data looks like this: Col1 Time..24hr. 1 1404 2 322 3 1945 4 1005 5 945 I'd then like to convert to 12hr so for '322' I'd like to make...

Disaggregate one row of data to multiple rows

r,excel,statistics,dataset,google-adwords
Goodafternoon! I am having some trouble with my dataset. I am using a Google AdWords export for data analysis and I want to fit a logit regression model to the data to determine whether an experiment I have conducted impacts the conversion. The problem is that the data is aggregated...

Group instances based on NA values in r

r,file,csv,instance,na
I am reading a csv file and unfortunately my dataframe has many missing values. A small snip is as following: df <- data.frame(Size= c(800, 850, 1100, 1200, 1000), Value= c(900, NA, 1300, 1100, NA), Location= c(NA, 'midcity', 'uptown', NA, 'Lakeview'), Num1 = c(2, NA, 3, 2, NA), Num2 = c(2,3,3,1,2),...

Aggregating data in R

r
user_id date datetime page 217568 6/12/2015 49:23.9 Vodafone | How to get in touch with Vodafone 135437 6/10/2015 43:35.7 My Vodafone – Manage your Vodafone Pay Monthly Account Online – Vodafone 196094 6/13/2015 33:39.4 Check the status of Vodafone’s mobile network in real-time 74197 6/6/2015 52:46.1 undefined 153501 6/5/2015 02:55.5...

R: Using the “names” function on a dataset created within a loop

r,paste,assign,names
I am using a for loop to read in multiple csv files and naming the datasets import1, import2, etc. For example: assign(paste("import",i,sep=""), read.csv(files[i], header=FALSE)) However, I now want to rename the variables in each dataset. I have tried the following: names(as.name(paste("import",i,sep=""))) <- c("xxxx", "yyyy") But get the error "target of...

r cumsum-like function for splitting dataframe

r,data.frame
Given the following dataframe: mydf <- data.frame(x=c(1:10,10:1),y=c(10:1,1:10)) How is it possible to split it such that each sub-dataframe will have consecutive values of one column which are greater than the other column? For example in mydf, the outcome that I am hoping for is spliting it into three dataframes: (y...

agrep string matching in R

r,string-matching,tm,agrep,qdap
I have two list of some product names. My problem is "Operating system" is matching with "system", "cooling system",etc. But it has to match only with "Operating","OS". Another example is "Key Board" should be matched with "key" or "KB" but not with "Mother Board" or just "Board". How to give...

Retreive names of elements of a list via lapply

r
In a list I want to find which element has some specific length and just print which element is this and to replace its values with the mean value: A <- c(10) B <- c(20, 30) C <- c(40) D <- c(50) ll <- list(A, B, C, D) names(ll) <-...

How to build a 'for' loop with input$i in R Shiny

r,loops,for-loop,shiny
In my shiny app, I build a a number of checkboxes using a for loop, like this: landelist <- c("Danmark", "Tjekkiet", "Østrig", "Belgien", "Tyskland", "Sverige", "USA", "Norge", "Island") landecheckbox <- c() for (land in landelist){ landechek <- paste0("<label class=\"checkbox inline\"><input id=\"", land, "\" type=\"checkbox\" checked><span>", land, "</span></label>") landecheckbox <- c(landechek,...

Appending a data frame with for if and else statements or how do put print in dataframe

r,loops,data.frame,append
How do I put what I printed in a dataframe with a for loop and if else statements? Basically, this code: list<-c("10","20","5") for (j in 1:3){ if (list[j] < 8) print("Greater") else print("Less") }) #[1] "Less" #[1] "Less" #[1] "Greater" Or should it be something more like this? f3 <-...

Grouped barplot in ggplot2 in R

r,ggplot2,bar-chart
I would like to make a grouped bar plot. An example of my data is as follows: site code year month gear total value 678490 2012 3 GL 13882 678490 2012 4 GL 50942 678490 2012 5 GL 54973 678490 2012 6 GL 63938 678490 2012 7 GL 23825 678490...

Error when Fitting a glmer with poisson error structure

r
I hope somebody can help me. I'm trying to conduct an analysis which examines the number of samples of Hymenoptera caught over an elevational gradient. I want to examine the possibility of a uni-modal distribution in relation to elevation, as well as a linear distribution. Hence I am including I(Altitude^2)...

R / SQL /Python : Extracting connected components from node-edge pairs

python,sql,r,graph,connected-components
I struggle to come up with a title that describes what I'm trying to solve, so please comment if you have a better title! The solution can be in R, Python, or SQL (Aster TeraData SQL to be exact, though a solution any SQL language is very helpful for learning...

Fitted values in R forecast missing date / time component

r,time-series,forecasting
I've been doing a variety of models in R with time series data (in XTS format) and I keep running into the same issue where there's no date / time component to the fitted values / forecasts and thus I can't graph them on the same graph as the original...

Constrained quadratic optimization with the quadProg library

r,mathematical-optimization,quadprog,quadratic-programming
I have a vector A of length N. Also I have N*N matrix C. I want to maximize following equation : minimize (- (w_transpose * A) + p * w_transpose * C * w) Where w is a vector of length N, with constraints that each w is non-negative and...

Limit the color variation in R using scale_color_grey

r,colors,ggplot2
Before I start, allow me to explain my graph: I have two Genotypes (WTB and whd) and each have two conditions (0 and 7), so I have four lines. Now, I want to make a plot where each variable and its condition is the same color. Anything with whd will...

R stops displaying maps

r,google-maps,ggmap
Few days ago I was familiarizing myself with displaying maps, plotting points on the map from http://rpubs.com/nickbearman/r-google-map-making Today, I have intermittent success in displaying maps. library(ggmap) map <- qmap('Anaheim', zoom = 10, maptype = 'roadmap') Outputs Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Anaheim&zoom=10&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false And when I go to the URL...

How (in a vectorized manner) to retrieve single value quantities from dataframe cells containing numeric arrays?

r,dataframes,vectorization
I've got a dataframe that includes columns like the one on the right here: lengthArray speed_max 1 4 24, 18, 24, 18 2 10 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 3 4 -999, -999, -999, -999 4 2 -999, -999 5 2 18, 18 6 1...

Fitting a subset model with just one lag, using R package FitAR

r,time-series
I am trying to fit a subset model with only lag 4. In the manual it's written "you must use p=c(0,0,0,4) since p=4 will fit a full AR(4)". I did this. #fit a subset model with just lag 4 Fit=FitAR(p=c(0,0,0,4), lag.max = "default", ARModel = "ARz") However, I get the...

how to call Java method which returns any List from R Language? [on hold]

java,r,rjava
How to call java method which returns list from R Language.

Correlate by levels of a variable in R

r,correlation
I would like to correlate two variables and have the output reported separately for levels of a third variable. My data are similar to this example: var1 <- c(7, 8, 9, 10, 11, 12) var2 <- c(18, 17, 16, 15, 14, 13) categories <- c(1, 2, 3, 1, 2, 3)...

How can I use a variable to get an Input$ in Shiny?

r,variables,csv,shiny
I am new to R and I am creating a shiny application to read a csv and filter data. I am reading the csv file, then creating dropdowns with a loop using the column names and the unique values: output$dropdowns <- renderUI({ if (is.null(x())) { return(NULL) } lapply(1:ncol(x()), function(i) {...

Am I using sapply incorrectly?

r,sapply
This code is suppose to take in a word, and compute values for letters of the word, based on the position of the letter in the word. So for a word like "broke" it's suppose to compute the values for the letter "r" and "k" strg <- 'broke' #this part...

Why do I get this error below while using the Cubist package in R?

r,regression,decision-tree,non-linear-regression
I have some personal dataset. So I split it into variable to predict and predictors. Following is the syntax: library(Cubist) str(A) 'data.frame': 6038 obs. of 3 variables: $ ads_return_count : num 7 10 10 4 10 10 10 10 10 9 ... $ actual_cpc : num 0.0678 0.3888 0.2947 0.0179...

dplyr multiple inputs from Shiny

r,shiny,dplyr
I have a Shiny app that takes input from radio button and then use that to perform filter to the data frame using dplyr in the server side. It works, but now I want to expand it to take multiple inputs to filter, and I have no idea how to...

a maximum value per rowname(1, 2, or A, B..) per multiple columns in R

r,max,duplicate-data
I want to a maximum value per rowname(1, 2, or A, B..) per multiple columns(samples). I have a matrix. id sample1 sample2 sample3 ... 1 16498 2416 12555.5 1 21282.5 3342 22202 2 18558 2308 na 2 17966 3047 na 2 398 176.5 na 3 347 227 201.5 3 604.5...

R: Matrix row operations

r
I have a matrix A that has a large number of rows and columns (below one example of such a matrix) that occasionally has a full row of 0 values (as in row 4 at this particular example). I want to have a function that checks all rows of A...

Skip some lines with fread

r,fread
I am interested to skip some lines of my data frame before the header names . How can i do it by skiping all the lines before ID_REF or if ID_REF is not present, check for the pattern ILMN_ and deleting all the lines keeping immediate first if not containing...

Deleting all the rows that have some missing values using R [duplicate]

r
This question already has an answer here: remove rows with NAs in data.frame 8 answers I am working with a csv data set with around 1 million records. I need to perform two operations on the data set: Prepare a dataset that do not have those rows that have...

How can I minimize this function in R?

r,function,optimization,mathematical-optimization
I'm attempting to write a formula that will determine a value of a that minimizes the function output myfun (i.e. a-fptotal). MWE: c <- as.matrix(c(.25,.5,.25)) d <- as.matrix(c(10000,12500,15000)) e <- 700 f <- 1.1 tr <- .30 myfun <- function(a) { b <- max(a-e,0) df <- data.frame(u1=c(c*b*.40),u2=c(c*b*.60)) df$year <- 1:nrow(df)...

Subsetting rows by passing an argument to a function

r,subset
I have the following data frame which I imported into R using read.table() (I incorporated read.table() within read_data() which is a function I created that also throw messages in case the file name is not written appropriately): > raw_data <- read_data("n44.txt") [1] #### Reading txt file #### > head(raw_data) subject...

An error while looping a linear regression

r,loops,data.frame,regression
I would like to run a loop that will run per each category of one of the variables and produce a prediction per each regression so that the sum of the prediction variable will be deduced from the target variable .Here Is my toy data and code: df <- read.table(text...

How to set ggvis to use canvas renderer by default?

r,shiny,ggvis,shinyapps
Is it possible to make a ggvis widget use the canvas renderer by default (instead of svg)?

Reading data from an XML File Using R

xml,r
From reading through, then trying a few previous examples on StackOverflow related to reading an XML file in R, it seems that due to the “jagged” nature of the following file I can’t use XPath related methods. https://www.dropbox.com/s/jz8sj2fifuobkva/Data.xml?oref=e&n=305307914 Therefore, it seems I need to use a combination of xmlToList() and...

randomly assign teachers to a school with dplyr or similar?

r,dplyr
Suppose I have a data frame with 8 schools and its characteristics, and another with 48 teachers and its characteristics. I can generate some fake data with the following code: library(dplyr) library(geosphere) set.seed(6232015) n.schools <-8 n.teachers <- 48 makeRandomString <- function(pre, n=1, length=12) { randomString <- c(1:n) # initialize vector...

Sort a List of date intervals using the first date

r
How can I sort a list of date intervals of the form: ll <- list(c('2015-03-01','2015-03-10'), c('2015-02-01','2015-02-10'),c('2015-01-01','2015-01-10')) by the first date, to get: list(c('2015-01-01','2015-01-10'), c('2015-02-01','2015-02-10'),c('2015-03-01','2015-03-10')) ...

Serial modification of objects in R

r,oop
I have a number of matrices of the same size: m1.m <- matrix(c(1,2,3,4), nrow=2, ncol=2) m2.m <- matrix(c(5,6,7,8), nrow=2, ncol=2) ... I want to set uniform column and row names to all of them. Currently I am doing it like this: new_col_names <- c("Col1","Col2") new_row_names <- c("Row1","Row2") change_names <- function(m,...

Regex to remove .csv in r

regex,r,stringr
this is going to be silly. I have a string like: word <- "dirtyboards.csv" I want to remove the csv part and get "dirtyboards". I am trying: require(stringr) str_extract(word, ".*[^.csv]") I get in return: "dirtyboard" . The "s" before the ".csv" goes missing. I know I can do , gsub(".csv",...