r

R beginner: column count by element

how do achieve the equivalent of awk '{print $1}' /tmp/data | sort | uniq -c for a particular column in R? Example: cat /tmp/data alama alama alama bbbb bbbb ccc alama bbbb bbbb awk '{print $1}' /tmp/data | sort | uniq -c 1 4 alama 4 bbbb 1 ccc i.e. count of every item in the column. Based on @Joshua's suggestio...

Extract the gradient from the deriv command

A colleague asked me the following question the other day. In the following piece of code, how do you extract the gradient: > x=5 > a = eval(deriv(~ x^3, "x")) > a [1] 125 attr(,"gradient") x [1,] 75 My answer was > attr(a, "gradient")[1] [1] 75 This syntax seems clunky to me. Is there a better way of extracting the gradien...

MLE for Naive Bayes in R

i am using naivebayes function of e1071 library of R like below: model <- naiveBayes(Species ~ ., data = iris) pred <- predict(model, iris[,]) my question is: how can i get maximum likelihood estimate for conditional probability distibution of this model? ...

Help speeding up a loop in R

basically i want to perform diagonal averaging in R. Below is some code adapted from the simsalabim package to do the diagonal averaging. Only this is slow. Any suggestions for vectorizing this instead of using sapply? reconSSA <- function(S,v,group=1){ ### S : matrix ### v : vector N <- length(v) L <- nrow(S) K <- N-L+1 ...

using R programming in java

We are working on a complex statistical project on java. The original code we did it in R programming language http://en.wikipedia.org/wiki/R_%28programming_language%29 is there a way to convert this code in java code (converter) or otherwise how can we use R in a java project ...

Multiple layers in ggplot2 with different datasets

Hi, I have a contour plot and I would like to add a geom_path with a different set of data over it. Right now I have the below code, but as soon as it gets to the geom_path, it overwrites the contour plot. Is there a way to prevent this from happening? v <- ggplot(pts, aes(theta_1, theta_2, z = z)) v + stat_contour(aes(colour = ..leve...

ggplot: boxplot sort

How do I sort a boxplot in ggplot? Here's what I'm trying to plot: qplot( row.names(pcaDF),pcaDF[,1],data=pcaDF,geom="boxplot") + coord_flip() and here's the structure of pcaDF > str(pcaDF) 'data.frame': 108 obs. of 1 variable: $ sort(plotdata[, 1], decreasing = F): num -5.89 -5.52 -4.66 -4.54 -3.92 ... ...

Create a panel from two data frames

I have two data frames -- one with stock closing prices arranged by date (rows) and ticker symbol (columns): > head(data.stocks) date A AAPL ABAT AB ABV 1 2010-10-04 32.59 278.64 3.65 26.42 125.89 2 2010-10-05 33.04 288.94 3.66 27.10 129.05 3 2010-10-06 32.67 289.19 3.59 26.99 129.90 4 2010-10-07 33.20 289.22 3.66 27...

Embed fix() function within .R script?

Good day all - I am looking for a way to embed the fix() function within a script. Basically, here's what I'm currently doing: I load a certain package. For example, "library(PerformanceAnalytics)" I call the "fix()" function to edit a couple functions within the loaded package. Example, "fix(VaR)". Then, using R's built-in editor, I ...

How to return a data.frame with a given name from a function?

Assume I have a function that reads data from a MySQL table, manipulates it and returns some data.frame. Note the function is just an example whose functionality does not matter itself..., E.g.: addRowSd <- function(table,con,pattern="^Variable") { dframe <- dbReadTable(con,table) cn <- colnames(dframe) qs <- subset(x, x %in% grep(pa...

R if with vectorized statements

x = seq(0.1,10,0.1) y <- if (x < 5) 1 else 2 I would want the if to operate on every single case instead of operating on the whole vector. What do I have to change? ...

excluding FALSE elements from a character vector by using logical vector

I manage to do the following: stuff <- c("banana_fruit","apple_fruit","coin","key","crap") fruits <- stuff[stuff %in% grep("fruit",stuff,value=TRUE)] but I can't get select the-not-so-healthy stuff with the usual thoughts and ideas like no_fruit <- stuff[stuff %not in% grep("fruit",stuff,value=TRUE)] #or no_fruit <- stuff[-c(stuff ...

Is it possible to run a SAS or R script from PL/SQL?

Any idea to run those kind of scripts from Oracle PL/SQL? Any solution would be appreciated. Thanks! ...

Script to convert Huge Three column table into table

I have a set of data (csv files) in the following 3 column format: A, B, C 3277,4733,54.1 3278,4741,51.0 3278,4750,28.4 3278,4768,36.0 3278,4776,50.1 3278,4784,51.4 3279,4792,82.6 3279,4806,78.2 3279,4814,36.4 And I need to get a three-way contingency table like: (sorry, this doesn't look completely good) A /B 4733 ...

Can (should) I inherit parts of a function in R?

I have two functions that start pretty similarly. Hence I wonder if this is the right moment to dive into inheritance in R. firstfunc <- function(table,pattern="^Variable") { dframe <- get(table) cn <- colnames(get(table)) qs <- subset(cn, cn %in% grep(pattern, cn, value=TRUE)) ..... } secondfunc <- function(table,pattern="^st...

How to catch an error/exception in R?

Possible Duplicate: Exception handling in R Hi Everyone, Does anyone have idea on how to catch an error or an exception in R. Thanks, ...

unknown timezone name in R strptime/as.POSIXct

Where can I find a list of all legal time names for R function as.POSIXct? 'as.POSIXct("1970-01-01",tz="CST")' will generate warning that "CST" (Central Standard Time) is unknown. Thanks ...

Creating and serializing / saving global variable from within a NAMESPACE in R

I would like to create a function within a package with a NAMESPACE that will save some variables. The problem is that when load is called on the .Rdata file it tries to load the namespace of the package that contained the function that created the .Rdata file, but this package need not be loaded. This example function is in a package...

Draw more than one function curves in the same plot

A way to draw the curve corresponding to a given function is this: fun1 <- function(x) sin(cos(x)*exp(-x/2)) plot (fun1, -8, 5) How can I add another function's curve (e.g. fun2, which is also defined by its mathematical formula) in the same plot? ...

R: Call matrixes from a vector of string names ??

Hello Imagine I've got 100 numeric matrixes with 5 columns each. I keep the names of that matrixes in a vector or list: Mat <- c("GON1EU", "GON2EU", "GON3EU", "NEW4", ....) I also have a vector of coefficients "coef", coef <- c(1, 2, 2, 1, ...) And I want to calculate a resulting vector in this way: coef[1]*GON1EU[,1]+coef[2]*GON...