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...
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...
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?
...
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
...
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
...
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...
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 ...
...
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...
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 ...
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...
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?
...
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 ...
Any idea to run those kind of scripts from Oracle PL/SQL? Any solution would be appreciated. Thanks!
...
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 ...
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...
Possible Duplicate:
Exception handling in R
Hi Everyone,
Does anyone have idea on how to catch an error or an exception in R.
Thanks,
...
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
...
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...
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?
...
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...