similar to: Error with lapply

Displaying 20 results from an estimated 20000 matches similar to: "Error with lapply"

2009 Jul 22
6
A question on operation on list
Hi, I have created a list object like that : x = vector("list") for (i in 1:5) x[[i]] = rnorm(2) x Now I want to do two things : 1. for each i, I want to do following matrix calculation : t(x[[i]]) %*% x[[i]] i.e. for each i, I want to get a 2x2 matrix 2. Next I want to get x[[1]] + x[[2]] +.... I did following : res=vector("list"); res = sapply(x, function(i) t(x[[i]]) %*%
2008 Sep 17
5
Loop on vector name
[My previous message rejected, therefore I am sending same one with some modification] I have 3 vectors with object name : dat1, dat2, dat3 Now I want to create a loop, like : for (i in 1:3) { cat(sd(dati)) } How I can do this in R? Regards,
2009 Mar 25
2
"[.data.frame" and lapply
Dear all, Trying to extract a few rows for each element of a list of data.frames, I'm puzzled by the following behaviour, > d <- lapply(1:4, function(i) data.frame(x=rnorm(5), y=rnorm(5))) > str(d) > > lapply(d, "[", i= c(1)) # fine, this extracts the first columns > lapply(d, "[", j= c(1, 3)) # doesn't do nothing ?! > > library(plyr)
2009 Mar 25
2
"[.data.frame" and lapply
Dear all, Trying to extract a few rows for each element of a list of data.frames, I'm puzzled by the following behaviour, > d <- lapply(1:4, function(i) data.frame(x=rnorm(5), y=rnorm(5))) > str(d) > > lapply(d, "[", i= c(1)) # fine, this extracts the first columns > lapply(d, "[", j= c(1, 3)) # doesn't do nothing ?! > > library(plyr)
2010 Jun 02
3
Seeking help on Vectorize()
Dear falks, here I have written following function :   fn <- Vectorize(function(x = 1:3, y = 3:6) {         x <- matrix(x, nrow=1)         y <- matrix(y, ncol=1)         dat <- apply(x, 2, function(xx) {                               apply(y, 1, function(yy) {                                   return(xx + yy) } ) })         return(dat)}, SIMPLIFY = TRUE) If I run this function, I got
2011 Jul 30
2
Applying ifelse() on different functions
Dear all, I am looking for some procedure to apply 'ifelse' condition on function. I have created an alternative to lapply() function with exactly same set of arguments named lapply1(), however with different internal codes. Therefore I want something like, if (some condition) then call lapply1() otherwise lapply() function. Ofcourse I can create a complete if()... else()... like of
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 <-
2011 Jun 26
2
String manipulation
Dear all, I have following kind of character vector: Vec <- c("344426", "dwjjsgcj", "123sgdc", "aagha123", "sdh343asgh", "123jhd51") Now I want to split each element of this vector according to?numeric?and string element. For example in the 1st element of that vector, there is no string element. Therefore I should get a vector of
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()
2010 Aug 01
2
How to understand whether a class is a S3 class?
Hi, is there any way to say: "this class 'x' is a S3 class?" For example what is the type of class "data.frame"? Is it a S3 class or S4? How can I get a complete list of all S3 classes currently available? Thanks,
2008 Oct 22
4
A matrix automation problem
[I am really sorry if it is double posted, I doubt me previous post could not reach forum due to some problem with net] Suppose I have a matrix : a = matrix(1:9, 3) >From this matrix, I construct 9 additional matrices : i = 1:9 bi = a * i Now combining all those 9 new matrices, I construct a final metrix as : c = b1 b4 b7 b2 b5 b8 b3 b6 b8 I want to automate this procedure for any
2008 Sep 09
2
make methods work in lapply - remove lapply's environment
I've defined my own version of summary.default, that gives a better summary for highly skewed vectors. If I call summary(x) the method is used. If I call summary(data.frame(x)) the method is not used. I've traced this to lapply; this uses the new method: lapply(list(x), function(x) summary(x)) and this does not: lapply(list(x), summary) If I make a copy of lapply, WITHOUT the
2010 May 07
1
Protecting elements within a function
Hi all, previously I submitted this thread through Nabble which seems fail therefore sending it again.... suppose I have written following function :  > fn = function(x) return(x+x^2)  > fn  function(x) return(x+x^2)  Here you see, if I type only the function name all inside information of this function are visible. Is there any way to protect them i.e. make them invisible?  Thanks
2008 Oct 15
2
Defining a "list"
Can anyone please tell me how to define a "list". Suppose I want to define a list object "result" with length n then want to fill each place of "result" with different objects. For e.g. i=1 result[1] = rnorm(1) i=2 result[2] = rnorm(2) ....................... i=n result[n] = rnorm(n) What would be the best way to do that? Regards,
2009 Jun 22
2
Help on creating a sequence of vectors
I want to create a number of vectors like : vec1 <- rnorm(1) vec2 <- rnorm(2) vec3 <- rnorm(3) and so on........... Here I tried following : for (i in 1:10) paste("vec", i, sep="") <- rnorm(i) However obviously that is not working. Here vectors I need to be seperated i.e I do not want to create a "list". How to modify above code? -- View this
2008 Oct 11
5
Extracting subset of a vector
I have 2 vecros : x<-c(100,96,88,100,100,96,80,68,92,96,88,92,68,84,84,88,72,88,72,88) x1 = sample(x, 5, replace=FALSE) Now i want to get remaining values of vector "x" those are not member of vector "x1". Can anyone please tell me how to do that?
2008 Mar 15
1
Fwd: Re: How to create following chart for visualizing multivariate time series
Thanks David, It is working. Holtman's also gave me a solution but, I wanted to have a color pallet for description of colors, that was not in his solution. However I need one small modification. If I want to plot only lower diagonal elements of 'dat' then how should I proceed? What I want is, to visualize only lower diagonal elements and having the color pallet on them only. Also
2007 Jun 12
3
Panel data
Dear all R users, I have a small doubt about panel data analysis. My basic understanding on Panel data is a type of data that is collected over time and subjects. Vector Autoregressive Model (VAR) model used on this type of data. Therefore can I say that, one of statistical tools used for analysis of panel data is VAR model? If you clarify my doubt I will be very grateful. Thanks and regards,
2018 May 25
2
how to make the code more efficient using lapply
Dear All, I have a following for-loop code which is basically intended to read in many excel files (each file has many columns and rows) in a directory and extract the some rows and columns out of each file and then combine them together into a dataframe. I use for loop which can do the work but quite slow. How to make it faster using lapply function ? Thanks in advance! temp.df<-c() #
2008 Mar 15
1
How to create following chart for visualizing multivariate time series
Let me take an artifical matrix : dat = matrix(rnorm(200*200), 200, 200) My goal is to visualize this matrix according to the procedure, described in previous mails. I took Mendelssohn's advice and got following advice : ?plot.im Z <- setcov(owin()) plot(Z) .................... etc However I can not reproduce this example in my problem. How I can change my data