r

R-thonic replacement for simple for loops containing a condition

I'm using R, and I'm a beginner. I have two large lists (30K elements each). One is called descriptions and where each element is (maybe) a tokenized string. The other is called probes where each element is a number. I need to make a dictionary that mapsprobes to something in descriptions, if that something is there. Here's how I'm going...

Stacked Area Histogram in R

I ran a Pig job on a Hadoop cluster that crunched a bunch of data down into something R can handle to do a cohort analysis. I have the following script, and as of the second to last line I have the data in the format: > names(data) [1] "VisitWeek" "ThingAge" "MyMetric" VisitWeek is a Date. ThingAge and MyMetric are integers. The...

installing R packages on ubuntu 8.1

preface: i'm an os x user coming to linux, so excuse my ignorance in advance i've installed R using synaptic and now i'm trying to install packages. i open R then try install.packages("some package") system tries to default to /site-library, then tell's me it's not writable then asks about making a personal library? should I just ma...

R: Manipulating a data frame with contents from a different data frame

Say I have a data frame with the contents: Trial Person Time 1 John 1.2 2 John 1.3 3 John 1.1 1 Bill 2.3 2 Bill 2.5 3 Bill 2.7 and another data frame with the contents: Person Offset John 0.5 Bill 1.0 and I want to modify the original frame based on the appropriate value from the second. I co...

Curve fitting in R using nls

I'm trying to fit a curve over (the tail of) the following data: [1] 1 1 1 1 1 1 2 1 2 2 3 2 1 1 4 3 2 11 6 2 16 7 17 36 [25] 27 39 41 33 42 66 92 138 189 249 665 224 309 247 641 777 671 532 749 506 315 292 281 130 [49] 137 91 40 27 34 19 1 I'm using the following function in...

R: chopping a string into a vector of character elements

I have an object containing a text string: x <- "xxyyxyxy" and I want to turn that into a vector with each element containing two letters: [1] "xx" "yy" "xy" "xy" it seems like the strsplit() should be my ticket, but since I have no regular expression foo, I can't figure out how to make this function chop the string up the way I wa...

Evaluating variable within R loop

I'm trying to iteratively generate some functions using a For Loop: # Create a list to hold the functions funcs <- list() funcs[] # loop through to define functions for(i in 1:21){ # Make function name funcName <- paste( 'func', i, sep = '' ) # make function func = function(x){x * i} funcs[[funcName]] = func ...

R - how to use contents of one vector as the symbol in a plot?

I have a two vectors of numbers of equal length. How do I plot the first vector while using the corresponding element in the second vector as the printing character? (Background: I sorted the first column and the second column holds the original indices. I want to use the indices as the printable character so that I can see which data po...

getting a hashmap in R using rJava

The title says is it all: I have a plain hashmap with numeric values and would like to retrieve its content, ideally in a list (but that can be worked out). Can it be done? ...

Is there a way to remove the border of the legend in ggplot2?

I'm using qplot to plot a function and I want to position the legend within the plot. I've used opts( legend.position = c(0.7,0.7) ) to move the legend where I want it to be. However there is a white border around the legend and that shows up on the gray background. For example: library(ggplot2) x = c(1:20) y = c(1:20) p <- qplo...

Datasets for Running Statistical Analysis on

What datasets exist out on the internet that I can run statistical analysis on? ...

Using ggplot2 how can I represent a dot and a line in the legend

Using ggplot2 I am plotting several functions and a series of points. I cannot figure out how to represent the points on the legend. I realize I need to use an aes() function, but I don't fully understand how to do this. I apologize that the example is so long, but I don't know how else to illustrate it. ## add ggplot2 library(ggplot2) ...

R - idiomatic way to deal with lists of data frames

I have 30 runs of data, each stored in a separate CSV file, runi.csv, i = 0:29. Let's say I want to collect them all into a list. Best way I know how to do this is runs = list() for (i in 1:30) { runs[[i]] = read.csv(paste("run", i-1, ".csv")); } Now let's further say that each of these data frames stored in the list has the same co...

R: Performing binary function to a column in a data frame.

Say I have a data frame with the contents: Trial Person 1 John 2 John 3 John 4 John 1 Bill 2 Bill 3 Bill 4 Bill and I want to transform this to Trial Person Day 1 John 1 2 John 1 3 John 2 4 John 2 1 Bill 1 2 Bill 1 3 Bill 2 4 Bill 2 I can ver...

How to subtract days in R?

I'm trying to build folders to store data pulls. I want to label the folders with the day of that data in the pull. Ex. I pull 5 days ago data from mysql i want to name the folder the date from 5 days ago. MySQL can easily handle date arithmetic. I'm not sure exactly how R does it. Should i just subtract the appropriate number of secon...

What best practices do you use for programming in R?

What are some good practices for programming in R? Since R is a special-purpose language that I don't use all the time, I typically just hack together some quick scripts that do what I need. But what are some tips for writing clean and efficient R code? ...

R: serialize objects to text file and back again

I have a process in R that creates a bunch of objects, serializes them, and puts them into plain text files. This seemed like a really good way to handle things since I am working with Hadoop and all output needs to stream through stdin and stdout. The problem I am left with is how to read these objects out of the text file and back in...

List of ggplot2 options?

After some research I found the way to prevent an uninformative legend from displaying ... + opts(legend.position = "none") Where can I find all the available "opts" for ggplot2? ...

In R, how do I set the first values of a long vector to the values of a shorter one?

In R, how can I overwrite the first values of a long vector with values obtained from a file, where the file contains possibly fewer values? Example: # fill with n=100 values vec1 <- runif(100) # read m values, where m <= n vec2 <- scan("myfile", sep="\n") # now want to set the first m values of vec1 # to the values in vec2 I coul...

Transposing JSON list-of-dictionaries for analysis in R

I have experimental data expressed as dicts of key-value pairs for each experiment. A set of related experiments is serialized as a list of these dicts in JSON. This is parseable in in R via the rjson package, but the data is loaded in a form which is challenging to analyze data <- fromJSON('[{"k1":"v1","k2":"v2"}, {"k1":"v3","k2":"v4"...