similar to: lapply - function with arguments

Displaying 20 results from an estimated 50000 matches similar to: "lapply - function with arguments"

2009 Aug 17
1
how to pass more than one argument to the function called by lapply?
Dear R helpers: I wonder how to pass more than one argument to the function called by lapply. For example, #R code below --------------------------- indf <- data.frame(id=I(c('a','b')),y=c(1,10)) #I want to add an addition argument cutoff into the function called by lapply. outside.fun <- function(indf, cutoff) { unlist(lapply(split(indf, indf[,'id']),
2010 Mar 08
1
lapply and list indexing basics
I have split my original dataframe to generate a list of dataframes each of which has 3 columns of factors and a 4th column of numeric data. I would like to use lapply to apply the fitdistr() function to only the 4th column (x$isi) of the dataframes in the list. Is there a way to do this or am I misusing lapply? As a second solution I tried splitting only the numeric data column to yield a list
2008 Nov 01
1
lapply item names
Dear all, I am using lapply to generate plots by applying a plot function to a list of dataframes. e.g. lapply(dataSet, FUN = plotFunction) Is there a way to call the list item number inside the plot function so that I can identify each graph? Thanks Chibisi [[alternative HTML version deleted]]
2008 Jul 18
2
with lapply() how can you retrieve the name of the object
In the following code, I'd like to be able to create a new variable containing the value of the names of the list. a <- data.frame(var.1 = 1:5) b <- data.frame(var.1 = 11:15) test.list <- list(a=a, b=b) # in this case, names(test.list) is "a" and "b" # and I'd like to use lapply() so that # I get something that looks like # var.1 var.2 # 1 a # 2
2008 Aug 02
2
lapply, sapply
Hello everybody, I have problem with a lapply command, which rather proves that I don't fully understand it. I want to extract from a list that consists of dataframes, the length of the first sequences from a given variable (its part of a simulation exercises). Below is code which does the job, but I think it should be possible to make it more compact. ### Example Data dat <-list()
2007 Apr 09
3
do.call vs. lapply for lists
Hi All, I'm trying to understand the difference between do.call and lapply for applying a function to a list. Below is one of the variations of programs (by Marc Schwartz) discussed here recently to select the first and last n observations per group. I've looked in several books, the R FAQ and searched the archives, but I can't find enough to figure out why lapply doesn't do what
2010 Aug 10
3
sapply/lapply instead of loop
Using the input below, can I do something more elegant (and more efficient) than the loop also listed below to pad strings to a width of 5? The true matrix is about 300K rows and 31 columns. ####################### #INPUT ####################### > temp DX1 DX2 DX3 1 13761 8125 49178 2 63371 v75 22237 3 51745 77703 93500 4 64081 32826 v72 5 78477 43828 87645 >
2010 Feb 04
1
replace a for loop with lapply or relative
Dear helpers. I often need to make dichotomous variables out of continuous ones (yes, I realize the problems with throwing away much of the information), but I then like to check the min and max of each category. I have the following simple code to do this that cuts each variable (x1,x2,x3) at the 90th percentile, and then prints the min and max of each category:
2009 Apr 26
2
eager to learn how to use "sapply", "lapply", ...
After a year my R programming style is still very "C like". I am still writing a lot of "for loops" and finding it difficult to recognize where, in place of loops, I could just do the same with one line of code, using "sapply", "lapply", or the like. On-line examples for such high level function do not help me. Even if, sooner or later, I am getting my R
2010 Feb 26
2
dramatic speed difference in lapply
So I have a function that does lapply's for me based on dimension. Currently only works for length(pivotColumns)=2 because I haven't fixed the rbinds. I have two versions. One runs WAYYY faster than the other. And I'm not sure why. Fast Version: fedb.ddplyWrapper2Fast <- function(data, pivotColumns, listNameFunctions, ...){ lapplyFunctionRecurse <- function(cdata, level=1,
2008 Nov 15
1
Rename objects based on list
Hi all, I am trying to find a way to rename R objects with names pulled from a vector of names. For example, I have a data frame, my.data.frame, and a list of names, my.names. My.names is simply the column names of my.data.frame. I want save the histogram with the column name as the name of the object. for (i in 1:ncol(my.data.frame) { tmp<-hist(my.data.frame[,i])
2005 Mar 18
4
passing arguments to FUN in lapply
Suppose I have a nx2 matrix of data, X, the following code generate density estimation for each column and plot them denlist <- apply(X, 2, density) par(mfrow=c(1,2)) lapply(denlist, plot) Does anyone know how to change the main title of each density plot to "var 1", "var 2" by passing optional argument "main"? I've tried lapply(denlist, plot,
2008 Aug 12
3
Fwd: [MORG] IMAP5 List
If anyone's interested, especially client developers. It's been a bit quiet there after the initial rush. Begin forwarded message: > From: Randall Gellens <randy at qualcomm.com> > Date: August 1, 2008 5:08:39 AM EDT > To: ietf-imapext at imc.org, morg at ietf.org > Subject: [MORG] IMAP5 List > > At the MORG BOF, a discussion as to if the proposed IMAP
2011 Aug 09
1
How to pass different arguments to a function within lapply()?
Hi all, I have a data frame called "rst", see below: ------------------------------------------------------------------------------------------ # This is a paste able example # In case you don't have "KernSmooth" package installed, please uncomment below line. # install.packages("KernSmooth") library(KernSmooth) rst <- data.frame(hsp = rnorm(23), dal =
2008 Aug 22
2
Combining multiple datasets
Hi, I've tried to figure this out using Intro to R and help(), to no avail - I am new at this. I'm trying to write a script that will read multiple files from a directory and then merge them into a single new data frame. The original data are in a tree-ring specific format, and so I've first used a function (read.rwl) from the dplR package to read each file, translate each into a
2007 May 18
3
lapply not reading arguments from the correct environment
Hello, I am facing a problem with lapply which I ''''think''' may be a bug. This is the most basic function in which I can reproduce it: myfun <- function() { foo = data.frame(1:10,10:1) foos = list(foo) fooCollumn=2 cFoo = lapply(foos,subset,select=fooCollumn) return(cFoo) } I am building a list of dataframes, in each of which I want to keep only column
2009 Nov 03
3
re ading tokens
Greetings, I am not familiar with processing text in R. Can someone tell me how to read each line of words as separate elements in a list? FE, I would like to turn: word1 word2 word3 word2 word4 into a list of length two with three character elements in the first list and two elements in the second. I know that this should be easy, but I am a little confused by the text functions. Thanks in
2007 Apr 20
1
simply this loop?
Hi, anyone interested in this: I tried to simply this loop with lapply or something but haven't figured it out: mapt = c("203929_s_at", "203930_s_at", "203928_x_at", "206401_s_at") mapt.combn <- lapply(1:4, function(i) combn(mapt, i)) out = list() k = 1 for (i in 1:length(mapt.combn)){ for (j in 1:ncol(mapt.combn[[i]])){ out[[k]] =
2008 Sep 27
1
Append selectively to components of a list
Dear R users: Is there a way to append selectively to components of a list (if possible, loops are to be avoided)? To illustrate the point, in the example below, I would like to append 99 to vector b of the list l. > l <- list(a=c(1), b=c(2,3), c=c(4,5,6)) > l $a [1] 1 $b [1] 2 3 $c [1] 4 5 6 As you may expect, the result should look like: > l $a [1] 1 $b [1] 2 3 99 $c [1] 4 5
2007 Jun 08
4
logical 'or' on list of vectors
Suppose I have a list of logicals, such as returned by lapply: Theoph$Dose[1] <- NA Theoph$Time[2] <- NA Theoph$conc[3] <- NA lapply(Theoph,is.na) Is there a direct way to execute logical "or" across all vectors? The following gives the desired result, but seems unnecessarily complex. as.logical(apply(do.call("rbind",lapply(Theoph,is.na)),2,"sum"))