r

Scoping and functions in R 2.11.1 : What's going wrong?

This question comes from a range of other questions that all deal with essentially the same problem. For some strange reason, using a function within another function sometimes fails in the sense that variables defined within the local environment of the first function are not found back in the second function. The classical pattern in ...

How does one searches efficiently the internet for [R,the statistical package] related questions?

Possible Duplicate: How to search for R materials? Dear all, I have recently picked up the task of programming in R. R is the statistical package / programming enviroment/language. But there is horror! Because searching for R related questions in, say, google will yield a lot of irrelevant matches simply because 'R' is, well,...

referencing to an index dataframe and retreiving data where matches occur

Hi there, I am new to R and keen to learn but am finding myself particularly stuck on what seems to be a relatively straightforward idea. I have 2 dataframes. The first (ants) is data pertaining to a number of sites, each site has a unique ID, some sites have more than one row of data. It look like this: Site Date T...

ggplot2: Viewing data generated for a plot

Frequently, I use functions to shape or numerically alter data that I'm passing to ggplot, so that I don't have to alter my data before putting it in. If I've saved a plot to a variable. Is there a way to see the actual data that I'm plotting? It's a sanity check. For example: c <- ggplot(mtcars, aes(factor(cyl))) + geom_bar() If ...

how to sample distributions, given n, distribution name, and parameters in a dataframe?

I have a dataframe: priors <- data.frame(dist = c('lnorm', 'beta', 'gamma'), a = c(0.5, 1, 10), b = c(0.4, 25, 4), n = c(100, 100, 100) ) and I would like to take n samples from the distribution with parameters a and b. I have written this fu...

How to include jar file when creating a R package?

I created a skeleton R package: lib jarFileHere.jar R r_code_file.R The r_code_file.R tries to references a class file in jarFileHere.jar: library("rJava") library("rjson") .onLoad <- function(libname, pkgname) { .jpackage(pkgname, lib.loc=libname) } .onLoad("packagename", "../lib/jarFileHere.jar") .jnew("com/test/ClassHere",...

Two stage least square in R

Hi, I want to run a two stage probit least square regression in R. Does anyone know how to do this? Is there any package out there? I know it's possible to do it using Stata, so I imagine it's possible to do it with R. Thanks in advance, Manoel Galdino ...

Scale y-axis (counts) in ggplot2 histogram

I am plotting a simple histogram of data sampled at, say, 10%. I want counts on the y-axis, but since the data are sampled, I want them to be scaled appropriately. If I were using base graphics, I'd do something like foo <- rnorm(50) foo.hist <- hist(foo,plot=F) foo.hist$counts <- foo.hist$counts * 10 plot(foo.hist) Is there an eas...

data collection for statistics: from web to a database

Hi, I'm a statistician by trade and I'd like some recommendations on how to set up a website that can collect data into a database. For personal use, I use Google Forms to collect data, and everything gets populated into a spreadsheet. However, this may not be appropriate in a more professional setting, especially when we have multipl...

In R, what is the difference between class ts and class timeSeries?

In R, what is the difference between class ts and class timeSeries? I think I am getting a problem in HoltWinters because of that. I'm getting: data(LakeHuron) x <- LakeHuron before <- window(x, end=1935) after <- window(x, start=1935) a <- .2 b <- 0 g <- 0 model <- HoltWinters(before, alpha=a, beta=b, gamma=g) "Error in decompose(ts(...

In R, how can you use Holt-Winters smoothing for a financial ("business-day")-based time series?

In R, how can you use Holt-Winters smoothing for a financial ("business-day")-based time series? (For example, a stock data time series has an irregular time index). ...

Trouble rbinding ts objects – only replacement of elements is allowed ?

Dear all, i´d like to rbind the prediction output of an arima model to my original ts series object in a melt type of data format. But I get the following error message that I dont understand: Error in [<-.ts(*tmp*, ri, value = c(12.2567768232753, -0.0141881223732589, : only replacement of elements is allowed Here´s some reproduc...

Create Lists from Character String

Hello all, Perhaps my brain is not working today but i cant figure out how to create a list from 2 character strings. I've currently got scale_lab [1] "Very Poor" "Poor" "Average" "Good" "Very Good" [6] "Don't Know" and scale_rep [1] "1" "2" "3" "4" "5" "9" So what I want to do is combine the two into a list so ...

Extracting text data from PDF files

Is it possible to parse text data from PDF files in R? There does not appear to be a relevant package for such extraction, but has anyone attempted or seen this done in R? In Python there there is PDFMiner, but I would like to keep this analysis all in R if possible. Any suggestions? ...

Histogram in R when using a binary value

I have data of students from several schools. I want to show a histogram of the percentage of all students that passed the test in each school, using R. My data looks like this (id,school,passed/failed): 432342 school1 passed 454233 school2 failed 543245 school1 failed etc' (The point is that I am only interested in the percent of s...

Scoping problem when sfApply is used within function (package snowfall - R)

Let me add another scoping problem in R, this time with the snowfall package. If I define a function in my global environment, and I try to use that one later in an sfApply() inside another function, my first function isn't found any more : #Runnable code. Don't forget to stop the cluster with sfStop() require(snowfall) sfInit(parallel=...

How to have ggplot histogram bins follow boxplot breaks

So far I have this: hist <- ggplot(Mydf, aes(x=Mydf$Avg.CPC)) breaks <- c(min(Mydf$Avg.CPC), median(Mydf$Avg.CPC), max(Mydf$Avg.CPC)) h <- hist + geom_bar(binwidth = 0.025, colour = "black", breaks = breaks) print(h) I get an error. It doesn't like having different widths. I would also like to have the Q1 and Q3 quartiles as breaks wh...

compute means of a group by factor

Is there a way that this can be improved, or done more simply? means.by<-function(data,INDEX){ b<-by(data,INDEX,function(d)apply(d,2,mean)) return(structure( t(matrix(unlist(b),nrow=length(b[[1]]))), dimnames=list(names(b),col.names=names(b[[1]])) )) } The idea is the same as a SAS MEANS BY statement. The function 'me...

recursion in a prime generator in R

I'm making a prime generator, and to make it more efficient, i'm trying to only test numbers against primes that I've already found rather than all numbers < sqrt of the number being tested. I'm trying to get a to be my list of primes, but i'm not sure how to make it recur inside my second for loop. I think this is only testing against...

algorithm of combn r

can anyone tell me whats the algorithm used for this function: http://stat.ethz.ch/R-manual/R-devel/library/utils/html/combn.html ? I want to implement the same for php, so any pointers to php implementation of the same function would also be helpful. Thanks. ...