r

Find cosine similarity in R

I'm wondering if there is a built in function in R that can find the cosine similarity (or cosine distance) between two arrays? Currently, I implemented my own function, but I can't help but think that R should already come with one :) Thanks, Derek ...

Avoiding seasonality assumption for stl() or decompose() in R

Hello everybody, I have high frequency commodity price data that I need to analyze. My objective is to not assume any seasonal component and just identify a trend. Here is where I run into problems with R. There are two main functions that I know of to analyze this time series: decompose() and stl(). The problem is that they both take a ...

Lattice: multiple plots in one window?

I'm trying to put multiple lattice plots in one window using levelplot by setting par(mfrow=c(2,1)) but it seems to be ignoring this. Is there a particular function for setting multiple plots in lattice? ...

How to allow multiple inputs from user using R?

For example, if I need that the user specifies the number of rows and columns of a matrix: PROMPT: Number of rows?: USER INPUT: [a number] I need that R 'waits' for the input. Then save [a number] into a variable v1. Next, PROMPT: Number of columns?: USER INPUT: [another number] Also save [another number] into a variable v2. At ...

Converting a dataframe to a vector (by rows)

I have a dataframe with numeric entries like this one test <- data.frame(x=c(26,21,20),y=c(34,29,28)) How can I get the following vector? > 26,34,21,29,20,28 I was able to get it using the following, but I guess there should be a much more elegant way X <- test[1,] for (i in 2:dim(test)[1]){ X <- cbind(X,test[i,]) } ...

creating a spreadsheet from an xml file

I am trying to convert a 120mb xml database of terrorist incidents (the first file for download available here http://wits.nctc.gov/Export.do) to spreadsheet form so i can merge it with other data and do statistical analysis. so far I have worked with stata, which is useless now because it wont read xml. the site offers smaller files by...

Row/column counter in 'apply' functions

What if one wants to apply a functon i.e. to each row of a matrix, but also wants to use as an argument for this function the number of that row. As an example, suppose you wanted to get the n-th root of the numbers in each row of a matrix, where n is the row number. Is there another way (using apply only) than column-binding the row num...

how to define fill colours in ggplot histogram?

I have the following simple data data <- structure(list(status = c(9, 5, 9, 10, 11, 10, 8, 6, 6, 7, 10, 10, 7, 11, 11, 7, NA, 9, 11, 9, 10, 8, 9, 10, 7, 11, 9, 10, 9, 9, 8, 9, 11, 9, 11, 7, 8, 6, 11, 10, 9, 11, 11, 10, 11, 10, 9, 11, 7, 8, 8, 9, 4, 11, 11, 8, 7, 7, 11, 11, 11, 6, 7, 11, 6, 10, 10, 9, 10, 10, 8, 8, 10, 4, 8, 5, 8, 7)...

Generate multiple graphics from within an R function

I'd like to spawn several graphics windows from within a function in R using ggplot graphics... testf <- function(a, b) { devAskNewPage(TRUE) qplot(a, b); # grid.newpage(recording = TRUE) dev.new() qplot(a, a+a); # grid.newpage(recording = TRUE) dev.new() qplot(b, b+b); } library(ggplot2) x <- rnorm(50) y <- rnorm(50) ...

Standard library function in R for finding the mode?

In statistical language R, mean() and median() are standard functions which do what you'd expect. mode() tells you the internal storage mode of the R object, not the value that occurs the most in its argument. But surely there is a standard library function that implements mode for a vector (or list). ...

What is the current state of Unit testing support in the R language

R is a statistics programming language. Part of R is the use of Packages, which themselves are written in the R language. Programming best practice includes the use of unit-testing to test the functions within these packages while they are being written and when they are used. I am aware of a few packages for unit testing within R, th...

find file name from full file path in R

Hi, I wondering if there is a quick way to extract the file name from the file full path in R (part of a file path) without the hassle of manipulating string? The equivalent in Java would be: File f = new File ("C:/some_dir/a") f.getName() //output a f.getFullAbsolutePath() //output c:/some_dir/a Thanks, Derek ...

Show frequencies along with barplot in ggplot2

I'm trying to display frequencies within barplot ... well, I want them somewhere in the graph: under the bars, within bars, above bars or in the legend area. And I recall (I may be wrong) that it can be done in ggplot2. This is probably an easy one... at least it seems easy. Here's the code: p <- ggplot(mtcars) p + aes(factor(cyl)) + ge...

Replace values in a dataframe based on another factor which contains NA's in R

I have a dataframe which contains (among other things) a numeric column with a concentration, and a factor column with a status flag. This status flag contains NA's. Here's an example df<-structure(list(conc = c(101.769, 1.734, 62.944, 92.697, 25.091, 27.377, 24.343, 55.084, 0.335, 23.280), status = structure(c(NA, NA, NA, NA, NA, NA,...

Measures of association in R -- Kendall's tau-b and tau-c

Are there any R packages for the calculation of Kendall's tau-b and tau-c, and their associated standard errors? My searches on Google and Rseek have turned up nothing, but surely someone has implemented these in R. ...

How can I collapse a dataframe by some variables, taking mean across others

I need to summarize a data frame by some variables, ignoring the others. This is sometimes referred to as collapsing. E.g. if I have a dataframe like this: Widget Type Energy egg 1 20 egg 2 30 jap 3 50 jap 1 60 Then collapsing by Widget, with Energy the dependent variable, Energy~Widget, would yield Widget Energy ...

[R] How to create a data.frame with a unknow number of columns ?

Hello I would like to create, in a function, a boucle to create a data.frame with a variable number of columns. WIth something like : a = c("a","b") b = c(list(1,2,3), list(4,5,6)) data.frame(a,b) I would like to get a data-frame like : a 1 2 3 b 4 5 6 Instead of I obtain : a 1 2 3 4 5 6 b 1 2 3 4 5 6 Thank you ! P...

In R: How can I know if my packages are up to date ?

Hi all, I am looking for a function that will tell me, for a list of packages, which of them is up to date and which is not (I need it so to trace back an R crash). Thanks, Tal ...

writing to a dataframe from a for-loop in R

I'm trying to write from a loop to a data frame in R, for example a loop like this> for (i in 1:20) { print(c(i+i,i*i,i/1))} and to write each line of 3 values to a data frame with three columns, so that each iteration takes on a new row. I've tried using matrix, with ncol=3 and filled by rows, but only get the last item from the loop...

Problem installing packages

I am installing Matrix on a Linux x86_64 multicore system. I receive a message: Warning message: In install.packages("Matrix", dependencies = TRUE) : package 'Matrix' is not available Sure enough, there are not many details on package troubleshooting. It appears that Matrix is available for x86_64, but it's not available in any repo...