similar to: with lapply() how can you retrieve the name of the object

Displaying 20 results from an estimated 70000 matches similar to: "with lapply() how can you retrieve the name of the object"

2011 Dec 22
1
ff object in lapply function
Hello. I'm using as.ffdf(mydataframe) to create ffdf objects inside an lapply loop and returning that. I then use crbind to combine the lapply results into allData. So...simplified flow looks like this. res <- lapply(1:nchunks, function(n) { blah blah with nth chunk mydataframe <- data.frame(blah blah) dat <-
2008 Sep 04
2
printing name of object inside lapply
Dear list members, I am trying, within a lapply command, to print the name of the objects in list or data frame. This is so that I can use odfWeave to print out a report with a section for each object, including the object names. I tried e.g. a=b=c=1:5 lis=data.frame(a,b,c) lapply( lis, function (z) { obj.nam <- deparse(substitute(z)) cat("some other text",obj.nam,"and so
2006 Aug 07
1
Running out of memory when using lapply
Hi all! I'm afraid I programmed something totally non-sensical and inefficient, but I can't figure out how to do it better. I have a list of ~ 40 000 characters. I want to take each element at a time, map it to a large data frame with hit=which(data.frame$column==elementFromList), then compute some statistic on data.frame[hit,] and return a result that consists of either 1) a list
2011 Sep 15
2
Returning the name of an object passed directly or from a list by lapply
Dear folks: Let?s suppose I want a function to print return the name of the object passed to it. > myname <- function(object) {out<-deparse(substitute(object)); out} This works fine on a single object: > O1 <-c(1:4) > myname(O1) [1] "O1" However it does not work if you use lapply to pass it the same object from a list: > O2 <-c(1:4) > object.list <-
2009 Oct 03
2
converting matrix of lists to a regular matrix
Take the following code: foo <- list() foo[[1]] <- list(a=1, b=2) foo[[2]] <- list(a=11, b=22) foo[[3]] <- list(a=111, b=222) result <- do.call(rbind, foo) result[,'a'] In this case, result[,'a'] shows a list. Is there a more elegant way such that result is a "regular" matrix of vectors? I imagine there are manual ways of going about this, but I was
2009 Feb 27
2
accessing and preserving list names in lapply
Sometimes I'm iterating over a list where names are keys into another data structure, e.g. a related list. Then I can't use lapply as it does [[]] and loses the name. Then I do something like this: do.one <- function(ldf) { # list-dataframe item key <- names(ldf) meat <- ldf[[1]] mydf <- some.df[[key]] # related data structure r.df <-
2008 Oct 17
2
function help
Hi everyone, I have dataset which I make a sample of it couple of times and each time I get the mean and standard deviation of each row for each sample. I have a function for that, which takes the name of the file and number of times to sample and then returns the mean and standard deviation for each row in each sample. Sample=function(name, n){
2009 Sep 28
4
How to assess object names within a function in lapply or l_ply?
Dear All, to produce output of several columns of a data frame, I tried to use lapply and also l_ply. In both cases, I would like to print a header line containing also the name of the respective column in the data frame. For example, I would like the following lapply(data.frame(a=1:3, b=2:4), function(x) print(deparse(substitute(x)))) to produce: [1] "a" [1] "b" and
2008 Jul 18
2
name returned by lapply
Hi there, I have a very simple question. If I execute the following code: test <- function(x) { name <- paste(x,"info_within_function") c(1,2,3,4,5) } ret <- lapply(1:10, test) , I end up with a list and each entry is just numbered [[1]], [[2]], ... [[10]] How can I force the result entries gettings names which are determined within the function. For this example,
2009 Aug 11
1
Passing a list object to lapply
Hello, I'm having difficulty passing an object name to a lapply function. Can somebody tell me the trick to make this work? #Works T13702 <- TRACKDATA[["13702.xls"]][["data"]] min(unlist(lapply(list(T13702), function(x) mdy.date(x[1, 2], x[1, 1], x[1, 3])))) 16553 #Works d<-2 assign(paste("T",substr(names(TRACKDATA)[d],1,(nchar(names(TRACKDATA)[d]
2008 Apr 18
2
using rbind() on multiple objects at once
Is there an efficient way to use rbind() with the five dataframes described in the following example: a <- c(1:5) list.foo <- lapply(a, function(x) data.frame(beta=a*rnorm(10), deta=a*rnorm(10))) big.data.frame <- rbind(list.foo[[1]], list.foo[[2]], list.foo[[3]], list.foo[[4]], list.foo[[5]]) #is there an easier method? For example, I naively thought you could do something like
2010 Aug 03
2
How to name matrices from a list with lapply ?
Dear list, I have a list of matrices : i1 <- matrix(1:10, nrow = 2, ncol = 5) i2 <- matrix(11:20, nrow = 2, ncol = 5) j <- list(i1 = i1, i2 = i2) I would like to attribute names to each dimension, for each matrix, as follows : $i1 B1 B2 B3 B4 B5 A1 1 3 5 7 9 A2 2 4 6 8 10 $i2 B1 B2 B3 B4 B5 A1 11 13 15 17 19 A2 12 14 16 18 20 However, I have to use lapply function and attribute
2007 Jun 13
1
passing (or obtaining) index or element name of list to FUN in lapply()
Hello everyone, I wonder if there is a way to pass the index or name of a list to a user-specified function in lapply(). For instance, my desired effect is something like the output of > L <- list(jack=4098,sape=4139) > lapply(seq(along=L),function(i,x) if(i==1) "jack" else "sape",x=L) [[1]] [1] "jack" [[2]] [1] "sape" >
2008 Oct 01
3
lapply where each list object has multiple parts
Hi. I have a list where each object in the list has multiple parts. I'd like to take the mean of just one part of each object. Is it possible to do this with lapply? If not, can you recommend another function? Thanks. eric > x1 <- c(0,1,2,3) > x2 <- c(7,8) > x3 <- c(2,6,6,8) > x4 <- c(4,8) > > Lst1 <- list(label1 = x1,label2 = x2) > Lst2 <-
2017 Aug 14
2
recursive lapply and keeping track of data
Hello, I'm writing a program that takes a tree in input (nested lists) and returns a copy of it replacing the leaves with something else (eg: a computation done on the original leaves). In the example below, the tree is composed by countries and cities, and the leaves (children of the cities) are vectors of numbers. The program takes this tree and replaces the vectors at the bottom by their
2010 Oct 08
3
Efficiency Question - Nested lapply or nested for loop
My data looks like this: > data name G_hat_0_0 G_hat_1_0 G_hat_2_0 G_0 G_hat_0_1 G_hat_1_1 G_hat_2_1 G_1 1 rs0 0.488000 0.448625 0.063375 1 0.480875 0.454500 0.064625 1 2 rs1 0.002375 0.955375 0.042250 1 0.000000 0.062875 0.937125 2 3 rs2 0.050375 0.835875 0.113750 1 0.877250 0.115875 0.006875 0 4 rs3 0.000000 0.074750 0.925250 2 0.897750 0.102000
2010 Jan 01
2
changing a list element's name during execution in lapply - possible?
Happy New Year, all! I want to do calculations on each element of a list l, but i want the returned list element to be named differently after the calculation. Is it possible to do the renaming somehow within the lapply call? l <- list(a=NA, b=NA) lapply(l, function(x) {names(x) <- "new name"; return(x) }) This does not work, any ideas? TIA
2013 Feb 15
10
reading data
Hi, #working directory data1 #changed name data to data1.? Added some files in each of sub directories a1, a2, etc. ?indx1<- indx[indx!=""] lapply(indx1,function(x) list.files(x)) #[[1]] #[1] "a1.txt"??????? "mmmmm11kk.txt" #[[2]] #[1] "a2.txt"??????? "mmmmm11kk.txt" #[[3]] #[1] "a3.txt"??????? "mmmmm11kk.txt" #[[4]] #[1]
2004 Jul 29
2
unwanted as.integer
> a <- sqlQuery(irrdb, "select count(field) from mytable where field = 1") > print(a) count(field) 1 8 > paste(a) [1] "as.integer(8)" Why the as.integer() representation? I later pass the result into this write.html.table(), and what I get is rows of as.integer()... when all I want is the integer itself. as.integer(31) as.integer(21)
2013 Jul 10
1
Kruskal.test
Hi, Please dput() your example dataset. dat1<- read.table(text="a?? a?? a?? a??? a??? b?? b??? b??? c??? c?? c??? c 2? 4??? 5?? 2??? 7??? 2?? 2??? 6??? 3??? 7?? 9??? 3 3? 3?? 4?? 1???? 6??? 8?? 1??? 3??? 5??? 2??? 6??? 3",sep="",header=FALSE,stringsAsFactors=FALSE) library(reshape) ?dat2<-melt(as.data.frame(t(dat1)),id.var="V1")[,-2]