r

how to download Big5 webpage using RCurl in R?

I am trying to use RCurl to download a webpage encoding in Big5, but the encoding is not right. I have been toying with the "encoding" parameters for a while but no use, any suggestions? Thanks. ...

15min time aggregation in R

Hello I have a zoo series in R. I can choose between a chron or a POSIXct index. How can I aggregate to 15min, taking the last element every 15min? I know how to aggregate daily, writing as.Date, but not how to aggregate every 15min. thanks. ...

R Sweave user defined function

Dear all, I´m writing up a small function giving a combined conditional density and empirical cumulative distribution plot. cdpl<-function(df,dep,indep){ attach(df) cdplot(dep~indep,xlab=deparse(substitute(indep)),ylab=deparse(substitute(dep))) g<-indep ec<-ecdf(indep) lines(knots(ec),as.numeric(names(table(ec(g)))...

creating a new variable using two columns when they satisfy certain conditions using R

Hi R experts, I am providing this example data to get my question across. aid=c(1,2,3,4,5,6,7,8,9,10) foson=c(0,1,2,0,6,9,0,0,3,0) fosof=c(0,0,2,3,0,0,0,5,0,0) data=data.frame(aid,foson,fosof) Now, I need to create a new variable (column) named data$hist with the following conditions: if foson==0 and fosof==0, then hist = 0; if foso...

Does the 'summarise' function in plyr still exist?

When using plyr, I often want to 1) perform an operation on only a subset of the variables and 2) name the output of the operation. For example: d = data.frame(sex=c("m","f","m","m","f","f"), age=c(30,20,15,50,10,40), weight=c(130,120,115,150,90,180)) ddply(d, .(sex), function(df) data.frame(age_mu = mean(df$age))) But this seems kind...

Integrating R and Flex

How can you integrate R and Flex (e.g. web server using rapache)? ...

labels including an expression in cut function used in a boxplot in R

I'm using the cut function to convert a numeric variable into a factor with two levels and using this in a boxplot like this: boxplot(Sp$Var1 ~ cut(Spt$Var5, breaks = c(0,50,100), labels =c("below 50%", "above 50%")), ...) I want to include sample size as "n=..." below each of the labels used in the cut function. I can get the sample ...

R: Add extra spacing between a subset of plots

I'm trying to output 6 figures into one image, in a 3x2 layout. I'd like to place extra space between the top row and the bottom two rows. Is this possible using R? I've looked through the documentation for par and plot and can't seem to find an appropriate option. Here's some example code: a = rnorm(100,100,10) b = rnorm(100,100,10) ...

[R] ggplot: specify aes by index

ggplot() + layer( data = diamonds, mapping = aes(x = carat, y = price), geom = "point", stat = "identity" ) In the above example, I am wondering if I can specify the parameters for the "aes" function by indexes. I know that carat and price correspond to the 1st and 8th elements in the names array of diamond. Can you advice why the ...

global variable in R function

I created a function for processing some of my data, like this: a <- "old" test <- function (x) { assign(x, "new", envir = .GlobalEnv) } test(a) But I can't see the a change from "old" to "new", I guess this is some of the "global variable", any suggestion? Thanks! ...

[r] LU decomposition with row pivot

The following function does not use row pivoting for LU decomposition. Is there an existing function in R that does LU decomposition with row pivot? > require(Matrix) > expand(lu(matrix(rnorm(16),4,4))) $L 4 x 4 Matrix of class "dtrMatrix" [,1] [,2] [,3] [,4] [1,] 1.00000000 . . ...

Basic input file parsing in R

I'm used to perl and new to R. I know you can read whole tables using read.table() but I wonder how can I use R to parse a single line from an input file. Specifically, what is the equivalent to the following perl snippet: open my $fh, $filename or die 'can't open file $filename'; my $line = <$fh>; my ($first, $second, $third) = split ...

Creating an R dataframe row-by-row

I would like to construct a dataframe row-by-row in R. I've done some searching, and all I came up with is the suggestion to create an empty list, keep a list index scalar, then each time add to the list a single-row dataframe and advance the list index by one. Finally, do.call(rbind,) on the list. While this works, it seems very cumber...

Multiply rows of matrix by vector?

I'm optimizing a function and I want to get rid of slow for loops. I'm looking for a faster way to multiply each row of a matrix by a vector. Any ideas? EDIT: I'm not looking for a 'classical' multiplication. Eg. I have matrix that has 23 columns and 25 rows and a vector that has length of 23. In a result I want to have matrix 25x23...

R: polynomial shortcut notation in nls() formula

With the linear model function lm() polynomial formulas can contain a shortcut notation like this: m <- lm(y ~ poly(x,3)) this is a shortcut that keeps the user from having to create x^2 and x^3 variables or typing them in the formula like I(x^2) + I(x^3). Is there comparable notation for the nonlinear function nls()? ...

Treatment of 'empty' values in R

This is a newbie question in R. I am importing a csv file into R using sqldf package. I have several missing values for both numeric and string variables. I notice that missing values are left empty in the dataframe (as opposed to being filled with NA or something else). I want to replace the missing values with an user defined value. Ob...

Intersecting Points and Polygons in R

Hi, I am working with shapefiles in R, one is point.shp the other is a polygon.shp. Now, I would like to intersect the points with the polygon, meaning that all the values from the polygon should be attached to the table of the point.shp. I tried overlay() and spRbind in package sp, but nothing did what I expected them to do. Could a...

R script line numbers at error?

I found this post from a year ago, and I'm using R version 2.11.1 (2010-05-31), but still getting error messages without line numbers. Any solution? ...

How to attach a simple data.frame to a spatialpolygondataframe in R?

Hi, I have (again) a problem with combining data frames in R. But this time, one is a spatial.polygon.data.frame(SPDF) and the other one is usual data.frame (DF). The SPDF has around 1000 rows the DF only 400. Both have a common column, QDGC Now, I tried oo <- merge(SPDF,DF, by="QDGC", all=T) but this only results in a normal data...

How to efficiently use Rprof in R?

I would like to know if it is possible to get a profile from R-Code in a way that is similar to matlab's Profiler. That is, to get to know which line numbers are the one's that are especially slow. What I acchieved so far is somehow not satisfactory. I used Rprof to make me a profile file. Using summaryRprof I get something like the fol...