r

Choropleth mapping issue in R

EDIT: I have realized the source of my problem. I only have count information for the counties which I have data for, which is less than the number of counties in the area I'm plotting against. It stands to reason that the problem lines of code are here: mapnames <- map("county",plot=FALSE)[4]$names colorsmatched <- d$colorBuckets [na....

R: getting "inside" environments

Given an environment object e: > e <environment: 0x10f0a6e98> > class(e) [1] "environment" How do you access the variables inside the environment? Just in case you're curious, I have found myself with this environment object. I didn't make it, a package in Bioconductor made it. You can make it, too, using these commands: library('GE...

In R , change the element of a vector

say, i have a vector which have thousands of elements. whats the R code if i wanna make the element between 100-200 become 0. or, how to count the length between two different value. for example, the share price, if i wanna know the length of time when the price is b/t 30-40. thx a lot ...

Manipulate conditional statement using regex in R

Hello, I am trying to manipulate a conditional string outputted from SAS into the right format for a conditional statement in R. Here is an example of the conditional outputted from SAS: . < var1_a<=80 and var2_a>50.8 I've written a function that handles some of the transformation necessary: conditonalsub <- function(x) { subnew <- ...

R: ggplot2, can I set the plot title to wrap around and shrink the text to fit the plot?

library(ggplot2) my_title = "This is a really long title of a plot that I want to nicely wrap \n and fit onto the plot without having to manually add the backslash n, but at the moment it does not" r <- ggplot(data = cars, aes(x = speed, y = dist)) r + geom_smooth() + #(left) opts(title = my_title) can I set the plot title to wrap a...

How would you program Pascal's triangle in R?

I am reading, on my own (not for HW) about programming, and one exercise involved programming Pascal's triangle in R. My first idea was to make a list and then append things to it, but that didn't work too well. Then I thought of starting with a vector, and making a list out of that, at the end. Then I thought of making a matrix, and ...

R: How to write out a data.frame so that I can paste it into SO for others to read?

I have a large data.frame displaying some weird properties when plotted. I'd like to ask a question about it on Stackoverflow, to do that I'd like to write the data.frame out in a form that I can paste it into SO and somebody else can easily run it and have it back into a data.frame object again. Is there an easy way to accomplish this...

How do you save the state of a computation in while using SNOW (or Multicore or ...)

From hard experience I've found it useful to occasionally save the state of my long computations to disk to start them up later if something fails. Can I do this in a distributed computation package in R (like SNOW or multicore)? It does not seem clear how this could be done since the master is collecting things from the slaves in a non...

R: disentangling scopes

Hi, My question is about avoiding namespace pollution when writing modules in R. Right now, in my R project, I have functions1.R with doFoo() and doBar(), functions2.R with other functions, and main.R with the main program in it, which first does source('functions1.R'); source('functions2.R'), and then calls the other functions. I've ...

Creating a facet_wrap plot with ggplot2 with less annotations than plots

I am using ggplot2 to plot a figure that contains nine facets. Each facet represents the relationship between two variables and I would like to annotate the facets that display statistically significant results with a star ''. This would result in only two of the nine facets with a ''. However, I end up with all nine facets displaying th...

prcomp error in R

I am using R. I want to run prcomp on a matrix. The code works fine with one installation of R on a Linux box but breaks on another identical (or so I thought) installation of R on a different Linux box. The codes are dataf = read.table("~/data/testdata.txt") pca = prcomp(dataf) The error msg on the bad instance is > dataf = read.tab...

Graphing perpendicular offsets in a least squares regression plot in R

I'm interested in making a plot with a least squares regression line and line segments connecting the datapoints to the regression line as illustrated here in the graphic called perpendicular offsets: http://mathworld.wolfram.com/LeastSquaresFitting.html I have the plot and regression line done here: ## Dataset from http://www.apsnet....

Enable scratch buffer to execute R code in emacs-ess

Hello, I have switched to using emacs-ess for my R code development and it is working great. I would like to be able to write some small R code I am using for debugging my R script into the scratch buffer, and be able to execute the scratch buffer code in the R process buffer. I've found how I could change the scratch buffer's mode to te...

Pass a data.frame column name to a function

I'm trying to write a function to accept a data.frame (x) and a column from it. The function performs some calculations on x and later returns another data.frame. I'm stuck on the best-practices method to pass the column name to the function. The two minimal examples fun1 and fun2 below produce the desired result, being able to perform ...

Summarising grouped records in a dataframe in R

Hello all, I have a data frame in R that looks like this: > TimeOffset, Source, Length > 0 1 1500 > 0.1 1 1000 > 0.2 1 50 > 0.4 2 25 > 0.6 2 3 > 1.1 1 1500 > 1.4 1 18 > 1.6 2 2500 > 1.9 2 ...

find contiguous stretches of equal data in a vector

I have a numeric vector, it contains patches of elements that are repeating, something like: R> data <- c(1,1,1,2,2,2,3,3,2,2,2,2,2,3,3,1,1,1,1,1) R> data [1] 1 1 1 2 2 2 3 3 2 2 2 2 2 3 3 1 1 1 1 1 R> I need to extract contiguous patches of elements equals to a specific value... but I'm only interested in the patch around a specif...

R : remove columns from dataframe where ALL values are NA

hello everybody! I'm having some trouble with my huge data frame and couldn't really resolve that question myself: The dataframe has some properties as columns and each row represents one data set. I've done some sanatizing to this dataframe (e.g. get rid of datasets which are not to be included in evaluation). (Whoever might be interest...

Summarising grouped records in a dataframe in R (...again)

Hello all, (I tried to ask this question earlier today, but later realised I over-simplified the question; the answers I received were correct, but I couldn't use them because of my over-simplification of the problem in the original question. Here's my 2nd attempt...) I have a data frame in R that looks like: "Timestamp", "Source", "...

Using functions and environments

Following the recent discussions here (e.g. 1, 2 ) I am now using environments in some of my code. My question is, how do I create functions that modify environments according to its arguments? For example: y <- new.env() with(y, x <- 1) f <- function(env,z) { with(env, x+z) } f(y,z=1) throws Error in eval(expr, envir, enclos) : ...

Create categorical variable in R based on range

I have a dataframe with a column of integers that I would like to use as a reference to make a new categorical variable. I want to divide the variable into three groups and set the ranges myself (ie 0-5, 6-10, etc). I tried cut but that divides the variable into groups based on a normal distribution and my data is right skewed. I have...