r

R: removing the last elements of a vector

Hello How can I remove the last 100 elements of a zoo series? I know the name[-element] notation but I can't get it work to substract a full section ...

Understanding array indexing in R

The R documentation on arrays states The values in the data vector give the values in the array in the same order as they would occur in FORTRAN, that is "column major order," with the first subscript moving fastest and the last subscript slowest. It then later gives a clarifying example of this by loading data into a two dimension...

What is an efficient way to map unique values of a vector to sequential integers?

I have a dataframe in R with a vector of non-sequential numbers (data$SiteID) that i would like to map to a vector of sequential numbers (data$site) to the unique values of data$SiteID. Within each site, I would like to map data$TrtID to 0 where data$TrtID == 'control' or to the next sequential integer, for the other unique data$TrtID's:...

colnames() Behaviour

I have a question about the colnames() function in the base package Let's say you have a data.frame, as follows: df <- data.frame(variable=letters[1:100], value=rnorm(100)) As one would expect, colnames(df[1]) returns: colnames(df[1]) [1] "variable" However, it doesn't seem that assignment is possible when calling a specific c...

Moving beyond R's optim function

I am trying to use R to estimate a multinomial logit model with a manual specification. I have found a few packages that allow you to estimate MNL models here or here. I've found some other writings on "rolling" your own MLE function here. However, from my digging around - all of these functions and packages rely on the internal optim...

Parallel optimization in R

This question came at the right time, as I'm struggling with optimization as well. I am aware of the different "normal" optimization routines in R, and I am aware of parallel packages like snow, snowfall, Rmpi and the likes. Yet, I didn't manage to get an optimization running in parallel on my computer. Some toy code to illustrate : f...

How can I plot my R Squared value on my scatterplot using R?

Hi all. This seems a simple question, so I hope its a simple answer. I am plotting my points and fitting a linear model, which I can do OK. I then want to plot some summary statistics, for example the R Squared value, on the plot also. I can only seem to get the R Squared value at the command line. Any advice; do I need to be looking at ...

Can install, but can't access, the pwr package in R

I'm having trouble getting access to the pwr. This is on Ubuntu Lucid Lynx, 64 bit. I'm installing pwr via packages.install('pwr'), and loading it via library(pwr), both of which appear successful. Strangely, I never get access to the pwr object in R. I tried installing it to /usr/local/lib/R/site-library and ~/R/x86_64-pc-linux-gnu-...

Reverse digits in R

How can you reverse a string of numbers in R? for instance, I have a vector of about 1000 six digit numbers, and I would like to know if they are palindromes. I would like to create a second set which is the exact reverse, so I could do a matchup. ...

Loading many files at once?

So let's say I have a directory with a bunch of .rdata files file_names=as.list(dir(pattern="stock_*")) [[1]] [1] "stock_1.rdata" [[2]] [1] "stock_2.rdata" Now, how do I load these files with a single call? I can always do: for(i in 1:length(file_names)) load(file_names[[i]]) but why can't I do something like do.call(load, file...

How can I get the output I want from a lubridate subtraction

I started using lubridate today. I have one variable called Started which is the date on which human sujbects enrolled in a study and another variable is called dos1 which is the date upon which the subject last had surgery. I want to work out how many months since their last surgery to the day of enrollment as.period(syrrupan$Started-s...

Remove HTML tags from string (R Programming)

Hello all, Is there an easy way to remove HTML tags from a character string in R? Currently I'm extracting out survey data from an XML document and for the title of the question have HTML from the survey design in it, like this. "Why did you give this performance question a low score?<br />" Any way to easily remove the <br />? An...

Sorting a boxplot based on median value

I'd like to use R to make a series of boxplots which are sorted by median value. Suppose then I execute: boxplot(cost ~ type) This would give me some boxplots were cost is shown on the y axis and the type category is visible on the x-axis: ----- ----- | | [ ] | | [ ] | | ----- ----- A ...

Read a list of files with R, each file contains a list of float numbers. what's the proper way to do it?

Read a list of files with R, each file contains a list of float numbers. what's the proper way to do it? I'v never used R before. Just want to try a little bit of R All files contains 121 float numbers, one number per line any words will help. Thanks! ...

How to plot sunrise over a year in R?

I have date that looks like this: "date", "sunrise" 2009-01-01, 05:31 2009-01-02, 05:31 2009-01-03, 05:33 2009-01-05, 05:34 .... 2009-12-31, 05:29 and I want to plot this in R, with "date" as the x-axis, and "sunrise" as the y-axis. ...

How to use acast (reshape2) within a function in R?

I tried to use acast from reshape2 within a self written function, but had the problem that acast did not find the data I send to it. Here is my data: library("reshape2") x <- data.frame(1:3, rnorm(3), rnorm(3), rnorm(3)) colnames(x) <- c("id", "var1", "var2", "var3") y <-melt(x, id = "id", measure = c("var1", "var2", "var3")) y ...

Strings as variable references in R

Hi all! R newbie question here. I have a list called dbdata. Normally I use it like this: myresults <- rlm(V001 ~ V002+V003, data=dbdata) However, I would like to make these references dynamic. I need to have something like this: var1 <- "V001" var2 <- "V002" var3 <- "V003" myresults <- rlm(var1 ~ var2+var3, data=dbdata) How would...

How read an analogue input in R? / or how to translate an existing Matlab code

I use a photoelectric cell to time an event in a response time experiment. It works well in Matlab, but I wanted to control it in R. Would any of you be able to help me translate the Matlab code to R? global AI ch0 AI = analoginput('nidaq', 1); AI.InputType = 'SingleEnded'; ch0 = addchannel(AI,0); Thanks! ...

Graph to compare two matrices in R

I have two matrices (of approximately 300 x 100) and I would like to plot a graph to see the parts of the first one that are higher than those of the second. I can do, for instance: # Calculate the matrices and put them into m1 and m2 # Note that the values are between -1 and 1 par(mfrow=c(1,3)) image(m1, zlim=c(-1,1)) image(m2, zlim=c...

ggplot: position="dodge" question

I have a fairly simple data.frame dput(x), below: x <- structure(list(variable = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"), top2 = c(0.51, 0.24, 0.55, 0.36, 0.56, 0.67, 0.36, 0.22, 0.48, 0.6, 0.59, 0.06, 0.04, 0.2, 0.26, 0.25), bottom = c(0.03, 0.05, 0.03, 0.01, 0.02, 0.03, 0.01, 0.05, 0.03, ...