r

(R) How to tell what is in one vector and not another?

In matlab there is a way to find the values in one vector but not in the other. for example: x <- c(1,2,3,4) y <- c(2,3,4) is there any function that would tell me that the value in x that's not in y is 1? ...

double threshold (on/off) with or without a for

the function I am writing specifies the behaviour of a physical switch: it should be turned ON if a value goes above an upper threshold and can go again OFF if it goes under the lower threshold. a similar logic would describe a normal thermostat in a household oven. obviously I want it to work on vectors, that's the whole point! so if...

converting an ftable to a matrix

Take for example the following ftable height <- c(rep('short', 7), rep('tall', 3)) girth <- c(rep('narrow', 4), rep('wide', 6)) measurement <- rnorm(10) foo <- data.frame(height=height, girth=girth, measurement=measurement) ftable.result <- ftable(foo$height, foo$girth) I'd like to convert the above ftable.result into a matrix with ro...

How can I read and parse the contents of a webpage in R

I'd like to read the contents of a URL (e.q., http://www.haaretz.com/) in R. I am wondering how I can do it ...

What's a robust method in R for importing from and exporting data to Excel?

I've used RODBC for some time to import Excel spreadsheets with mostly good results. However I have had no luck writing to an Excel spreadsheet. Also are there favorable differences using the xlsx format with Excel2007? ...

Plotting functions in R

Is there a way of overlaying a mathematical function on top of data using ggplot? ## add ggplot2 library(ggplot2) # function eq = function(x){x*x} # Data x = (1:50) y = eq(x) # Make plot object p = qplot( x, y, xlab = "X-axis", ylab =...

R + user defined function

I have a situation in which I have to give a formula as input to the nls() function. I have data which is between time and variance. For example: Time Variance 1 0.15 2 0.23 3 0.67 4 0.85 Now I am using the formula Vt = ((1-e^kt)/kt)) (q^2)/2k, where Vt is the variance at time t. I have the two variables (k,q) in the above ...

Change specific column values in R

I have a table where there are "NA"s littered all over the place in one column in particular. I want to replace every instance of "NA" with something else -- say, the number 1. How should I do that? ...

R performance with data reshaping

I am trying to reshape a data frame in R and it seems to have problems using the recommended ways of doing so. The data frame has the following structure: ID DATE1 DATE2 VALTYPE VALUE 'abcd1233' 2009-11-12 2009-12-23 'TYPE1' 123.45 ... VALTYPE is a strin...

working with sequences of different length

a function I wrote extracts timestamps from a XML document. Timestamps are coupled to events, which are repeated elements of the series element. series elements have a variable amount of events, so my function returns a data.frame (if the series have the same length). in general it returns a more generic list and I want it to work wit...

Auto fit labels in R boxplot

We are currently using R to automatically generate various kinds of boxplots. The problem we have is that the length our labels varies considerably between different plots and classes in one plot. Is there a way to automatically adjust the plot so that all the labels will fit it nicely? Specifying a worst case mar isn't feasible because...

Read text file in R and convert it to a character object

Hi all, I'm reading a text file like this in R 2.10.0 248585_at 250887_at 245638_s_at AFFX-BioC-5_at 248585_at 250887_at 264488_s_at 245638_s_at AFFX-BioC-5_at AFFX-BioC-3_at AFFX-BioDn-5_at 248585_at 250887_at Using the command clusters<-read.delim("test",sep="\t",fill=TRUE,header=FALSE) Now, I must pass every row in this ...

multiple transform on df with plyr

I have a df and I want to do multiple transform on it with plyr: idplot / idtree / species / condition / dbh_cm / h_m / hblc_m CalcG <- function (df) transform(df, g_m2 = pi * (dbh_cm^2)/40000) CalcHD <- function (df) transform(df, hd = h_m / dbh_cm) ... Can be done in one function? Many thanks. ...

R function that returns a string literal

I have a vector: c(1,2,3) Calling print() on this value gives "[1] 1 2 3" Is there a function that takes a vector and gives the string "c(1,2,3)"? ...

Import data into R with an unknown number of columns?

I'm trying to read a text file with different row lengths: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 To overcome this problem, I'm using the argument fill=TRUE in read.table, so: data<-read.table("test",sep="\t",fill=TRUE) Unfortunately, to assess the maximum row length, read.table reads ...

Plot multiple sets of points in R

I have multiple sets of xy pairs that I want to plot. I want each set of xy pairs to be connected by a line. In other words the goal is to have multiple experimental instances each approximated by a line plotted on one plot. Also how would I colour the lines differently? The plot function does what I want, but takes on one set of xy ...

Best practices for storing and using data frames too large for memory?

I'm working with a large data frame, and have run up against RAM limits. At this point, I probably need to work with a serialized version on the disk. There are a few packages to support out-of-memory operations, but I'm not sure which one will suit my needs. I'd prefer to keep everything in data frames, so the ff package looks encouragi...

Problem with reshape in R

Hi I have a dataframe that I want to reshape; my reshape code: matchedlong <- reshape(matched, direction = 'long', varying = c(29:33, 36:3943), v.names = c("Math34", "TFCIn"), times = 2006:2009, idvar = "schoolnum") in "matched" columns 36 to 39 are logical (TRUE FAL...

distributing R package containing unit tests

so I decided I would put my few R functions into a package and I'm reading/learning Writing R Extension. it obviously complains about an amount of things I'm not doing right. after enough googling, I'm firing a few questions here, this one is about testing style: I am using RUnit and I like having tests as close possible to the code be...

R CMD check complains about unexpected files in man

this sounds like a silly problem: I'm putting my R code into a package and R CMD check src complains about the .Rd~ backup files being produced by Emacs. * checking package subdirectories ... WARNING Subdirectory 'man' contains invalid file names: read.PI.Rd~ write.PI.Rd~ the documentation says: »In addition [...] files [...] wit...