lapply

Can you use the lapply() function to alter the value of input?

I was wondering whether it is possible to use the lapply() function to alter the value of the input, similar to: a1<-runif(100) a2<-function(i){ a1[i]<-a1[i-1]*a1[i];a1[i] } a3<-lapply(2:100,a2) I'm looking for something akin to a for() loop, but using the lapply() infrastructure. I haven't been able to get rapply() to do this. The r...

Subset a data.frame by list and apply function on each part, by rows

This may seem as a typical plyr problem, but I have something different in mind. Here's the function that I want to optimize (skip the for loop). # dummy data set.seed(1985) lst <- list(a=1:10, b=11:15, c=16:20) m <- matrix(round(runif(200, 1, 7)), 10) m <- as.data.frame(m) dfsub <- function(dt, lst, fun) { # check whether dt is `...

How to create a column containing a string of stars to inidcate levels of a factor in a data frame in R

(second question today - must be a bad day) I have a dataframe with various columns, inculding a concentration column (numeric), a flag highlighting invalid results (boolean) and a description of the problem (character) df <- structure(list(x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), rawconc = c(77.4, 52.6, 86.5, 44.5, 167, 16.2, 59.3, 123,...

R: How to tell lapply to ignore an error and process the next thing in the list?

I have an example function below that reads in a date as a string and returns it as a date object. If it reads a string that it cannot convert to a date, it returns an error. testFunction <- function (date_in) { return(as.Date(date_in)) } testFunction("2010-04-06") # this works fine testFunction("foo") # this returns an erro...

R question. Using lappy on a data.frame and creating new variables w/ output.

Hello, I have 13 quantitative variables in a data.frame (called 'UNCA'). The variables are named q01_a, q01_b, ...q01_m. I want to create 13 new variables that have the same values but are coded as a factor. I would like to name these 13 new variables q01_a.F, q01_b.F, ...q01_m.F. Any help would be greatly appreciated! ...

How to replace a column in R? strange behavior with dates...

Dear all, I am trying to convert a uncommon date format into a standard date. Basically I have a dataset that contains a period with semiannual frequency formatted like: 206 denoting the second half of 2006, 106 denoting the first half and so forth. In order to rearrange it to 2006-06-01 respectively 2006-01-01, i have written a small ...

R: Creating lapply() type test cases

I've been working on code to create a parallel lapply() type function that uses Amazon's Elastic Map Reduce engine as the 'grid' for processing (yes, it's a mapper with no reducer). After I get the code stable I'll abstract it as a foreach backend. But first I need to build tests to test the code I have. What would be some good test ca...

setting levels inside lapply loop in r

Dear all, I´m trying to clean the factor variables in a dataframe from trailing spaces. However the levels assignment doesnt work inside my lapply function. rm.space<-function(x){ a<-gsub(" ","",x) return(a)} lapply(names(barn),function(x){ levels(barn[,x])<-rm.space(levels(barn[,x])) }) Any ideas how I can assign l...

How to return a data.frame with a given name from a function?

Assume I have a function that reads data from a MySQL table, manipulates it and returns some data.frame. Note the function is just an example whose functionality does not matter itself..., E.g.: addRowSd <- function(table,con,pattern="^Variable") { dframe <- dbReadTable(con,table) cn <- colnames(dframe) qs <- subset(x, x %in% grep(pa...