[R] apply function across dataframe columns for non-exclusive groups

2015-10-21 Thread Alexander Shenkin
Hello all, I've been banging my head over what must be a simple solution. I would like to apply a function across columns of a dataframe for rows grouped across different columns. These groups are not exclusive. See below for an example. Happy to use dplyr, data.table, or whatever. Any

Re: [R] apply function across dataframe columns for non-exclusive groups

2015-10-21 Thread Jeff Newmiller
The calculation appears to be sum(a)/(sum(a)+sum(b)). library(dplyr) library(tidyr) result <- ( this_df %>% gather( group, truth, -c(a,b) ) %>% group_by( group, truth ) %>% summarise( calc = sum(a)/(sum(a)+sum(b)) ) %>% as.data.frame )

Re: [R] Apply Function to Columns

2014-10-24 Thread Sarah
Le 24 oct. 2014 à 09:23, Sarah sarahschmi...@gmail.com a écrit : Thank you very much, it helped a lot! I just have another question know. I want to make plot for every species. I just add the function « plot correlog » to the previous function and I have now the following script:

Re: [R] Apply Function to Columns

2014-10-24 Thread Rui Barradas
Hello, Please cc the list, the odds of getting more and better answers are greater. And you should tell us from what package do the function plot.correlog comes. library(what)? As for your question, assuming you want to save your plots as PNG files, you could do something like the following.

[R] Apply Function to Columns

2014-10-23 Thread Sarah
Hello List, I have a database which consist of 912 plots. For each plot, I have the presence/absence information of 260 species of plants and also 5 different environmental variables (ddeg, mind, srad, slp, topo). The dataframe looks like this: Plot_NumberX Y ddeg

Re: [R] Apply Function to Columns

2014-10-23 Thread Rui Barradas
Hello, Yes, you can use lapply. Maybe something like the following. Note that the result is a list with one member per species. (Untested). ddeg.correlog.list - lapply(9:11, function(p) correlog(plant[plant[,p]==1, 2], plant[plant[,p]==1, 3], plant[plant[,p]==1, 4])) Hope this helps,

[R] apply function to multiple list arguments

2014-10-14 Thread Carlos Nasher
Hi R helpers, I'm struggling how to apply a function to multiple lists. My function uses a dataframe, a list of parameters and a fixed value as arguments. Now I want to apply that function to several dataframes contained in list, with several lists of parameters (also contained in a list) and the

Re: [R] apply function to multiple list arguments

2014-10-14 Thread Rui Barradas
Hello, Have you tried mapply(f, list_df, list_par, MoreArgs = list(z = fix), SIMPLIFY = FALSE) ? Hope this helps, Rui Barradas Em 14-10-2014 19:42, Carlos Nasher escreveu: Hi R helpers, I'm struggling how to apply a function to multiple lists. My function uses a dataframe, a list of

Re: [R] Apply function to one specific column / Alternative to for loop

2013-11-17 Thread Stageexp
Hi, Try: indx - grep(Test,test_df[,1])  ##assuming that there is some pattern  res - within(test_df[-indx,],titel - rep(test_df$titel[indx], diff(c(indx,nrow(test_df)+1))-1)) ## If you need to change the class res[] - lapply(res,function(x) if(any(grepl([[:alpha:]],x))) as.character(x) else

[R] Apply function to one specific column / Alternative to for loop

2013-11-16 Thread Stageexp
Hi guys, I am a total newbie to R, so I hope this isn't a totally dumb question. I have a dataframe with a title in one row and the corresponding values in the next rows. Let's take this example: test_df - data.frame(cbind(titel = , x = 4:5, y = 1:2)) test_df = rbind(cbind(titel=1.Test, x=, y=),

Re: [R] Apply function to one specific column / Alternative to for loop

2013-11-16 Thread umair durrani
This might be of some use : http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r/ Umair Durrani email: umairdurr...@outlook.com Date: Sat, 16 Nov 2013 07:30:29 -0800 From: ron...@gmx.net To: r-help@r-project.org Subject: [R] Apply function to one specific column

Re: [R] Apply function to one specific column / Alternative to for loop

2013-11-16 Thread arun
Hi, Try: indx - grep(Test,test_df[,1])  ##assuming that there is some pattern  res - within(test_df[-indx,],titel - rep(test_df$titel[indx], diff(c(indx,nrow(test_df)+1))-1)) ## If you need to change the class res[] - lapply(res,function(x) if(any(grepl([[:alpha:]],x))) as.character(x) else

Re: [R] Apply function to every 20 rows between pairs of columns in a matrix

2013-11-11 Thread arun
HI, It's not very clear. set.seed(25) dat1 - as.data.frame(matrix(sample(c(A,T,G,C),46482*56,replace=TRUE),ncol=56,nrow=46482),stringsAsFactors=FALSE)  lst1 - split(dat1,as.character(gl(nrow(dat1),20,nrow(dat1 res - lapply(lst1,function(x) sapply(x[,1:8],function(y) sapply(x[,9:56],

Re: [R] Apply function to every 20 rows between pairs of columns in a matrix

2013-11-11 Thread arun
Hi, May be this what you wanted. res2 - lapply(row.names(res[[1]]),function(x) do.call(rbind,lapply(res,function(y) y[match(x, row.names(y)),])))  length(res2) #[1] 48  dim(res2[[1]]) #[1] 2325    8 A.K. On Monday, November 11, 2013 10:20 PM, Yu-yu Ren renyan...@gmail.com wrote: Thank you

Re: [R] Apply function to every 20 rows between pairs of columns in a matrix

2013-11-11 Thread arun
HI, set.seed(25) dat1 - as.data.frame(matrix(sample(c(A,T,G,C),46482*56,replace=TRUE),ncol=56,nrow=46482),stringsAsFactors=FALSE)  lst1 - split(dat1,as.character(gl(nrow(dat1),20,nrow(dat1 res - lapply(lst1,function(x) sapply(x[,1:8],function(y) sapply(x[,9:56], function(z)

Re: [R] Apply function to do pairwise calculation

2013-10-01 Thread arun
. - Original Message - From: Amanda Li amand...@uchicago.edu To: r-help@r-project.org Cc: Sent: Monday, September 30, 2013 10:55 AM Subject: [R] Apply function to do pairwise calculation Hello, I want to do pairwise calculation, but I am not sure how to do so. i.e. I have a correlation

[R] Apply function to do pairwise calculation

2013-09-30 Thread Amanda Li
Hello, I want to do pairwise calculation, but I am not sure how to do so. i.e. I have a correlation matrix M 200*200. Namely colnames(M)=rownames(M). In addition, colnames(M) is one of A, B, C, D. I want to first sort the matrix M into 16 modules according to colnames and rownames, and then

Re: [R] Apply function to do pairwise calculation

2013-09-30 Thread Adams, Jean
Amanda, If I understand what you're trying to do, this example might help you. M - structure(c(0.66, 0.05, -0.93, -0.61, 0.65, -0.25, 0.23, -0.89, 0.37, 0.38, -0.91, 0.91, -0.05, -0.65, -0.94, 0.73, -0.88, 0.25, 0.04, -0.89, -0.47, -0.46, 0.86, -0.29, 0.92, 0.22, 0.77, -0.98, -0.56, 0.11,

[R] apply function within different groups

2013-05-23 Thread Estefanía Gómez Galimberti
Hi, I have a very big data frame and I would like to apply a function to one of the columns within different groups  and obtain another dataframe My data frame is like this: group var1 var2 myvar group1 1 a 100 group2 2 b 200 group2 34 c 300 group3 5 d 400 group3 6 e 500 group4 7 f 600

Re: [R] apply function within different groups

2013-05-23 Thread arun
group1  NA #2 group2 299 #3 group2 300 #4 group3 499 #5 group3 500 #6 group4  NA A.K. - Original Message - From: Estefanía Gómez Galimberti tef...@yahoo.com To: r help help r-help@r-project.org Cc: Sent: Thursday, May 23, 2013 11:30 AM Subject: [R] apply function

Re: [R] apply function within different groups

2013-05-23 Thread Estefanía Gómez Galimberti
...@yahoo.com To: Estefanía Gómez Galimberti tef...@yahoo.com Cc: R help r-help@r-project.org Sent: Thursday, May 23, 2013 12:48 PM Subject: Re: [R] apply function within different groups Hi, May be this helps: dat1- read.table(text= group var1 var2 myvar group1 1 a 100 group2 2 b 200 group2 34 c 300

Re: [R] apply function within different groups

2013-05-23 Thread arun
    7    f   600  NA A.K. From: Estefanía Gómez Galimberti tef...@yahoo.com To: arun smartpink...@yahoo.com Cc: R help r-help@r-project.org Sent: Thursday, May 23, 2013 12:08 PM Subject: Re: [R] apply function within different groups Thanks a lot!!! It works

Re: [R] apply function within different groups

2013-05-23 Thread arun
: Thursday, May 23, 2013 1:01 PM Subject: Re: [R] apply function within different groups Hi, No problem. Try: dat2-within(dat1,f_myvar-ave(myvar,group,FUN=mifunc))  dat2 #   group var1 var2 myvar f_myvar #1 group1    1    a   100  NA #2 group2    2    b   200 299 #3 group2   34    c   300

Re: [R] apply function over same column of all objects in a list

2012-08-03 Thread gail
Excellent, thank you to both of you!!! Gail -- View this message in context: http://r.789695.n4.nabble.com/apply-function-over-same-column-of-all-objects-in-a-list-tp4638681p4639009.html Sent from the R help mailing list archive at Nabble.com. __

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread arun kirshna [via R]
Hello Gail, The dat5 was in earlier post. I forgot to paste it. Now, your question regarding individual cases, This is a case that includes nonnumeric columns list1-list(data.frame(x=1:6,y=c(4,5,3,2,1,8),bw=c(4,18,12,3,4,9),nn=paste0(a,1:6)))

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread arun kirshna [via R]
Hi, I am not able to read your dataset. Getting errors. source(list1.txt) Error in paste(locuteur, i, sep = ) : object 'locuteur' not found Please check. A.K. __ If you reply to this email, your message will be added to the discussion below:

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread gail
Arun, I see you've defined dat5 in your later message; however the same applies as to the above: the code doesn't work if the list contains non-numerical elements. Gail -- View this message in context:

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread gail
Well but the whole point was to be able to plug in a single command and get the result for all sublists. If I have to create a new list and type in each sublist and the column I'm interested in, that completely defeats the purpose, I might as well just ask for max(sublist$coconut) for each

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread gail
Michael, here is the data I am working with. I've shortened it without, I trust, changing its nature. I am not using any package other than what is pre-installed. Many thanks, Gail dput(list.example) list (structure(list( x = c(379.455895016957, 380.116796110287, 380.777697203618,

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread gail
Hi, my dataset is the result of the function density on another set of data. It refers to that data in its variable call, though since all the results are actually reproduced (except that I've removed all rows bar 10), I am not sure why R still needs it. But I've understood now why your code

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread arun kirshna [via R]
Hello, If I understand it now, can't you do this: Individual cases: list4-list(mango[[1]],banana[[1]],pineapple[[1]]) b-list() for(i in 1:3){ b[[i]]-list() b[[i]]-lapply(list4[[i]],FUN=function(x)x[which.max(x)]) } b1-data.frame(do.call(rbind,b)) row.names(b1)-c(mango,banana,pineapple) b1

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread Eik Vettorazzi
I still try to figure out, what you are finally asking for, but maybe res-sapply(list4,function(x)max(x[[1]]$coconut)) names(res)-c(mango, banana, pineapple) res max(res) is worth a try? Especially i did not get the point of doing something like x[which.max(x)] because this is in any case just

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread arun kirshna [via R]
Hi Elk, Thanks for the input. Somehow, I got entangled with which.max. I should have used max. lapply(list4,function(x) max(x[[1]]$coconut)) [[1]] [1] 6 [[2]] [1] 18 [[3]] [1] 9 max(unlist(lapply(list4,function(x) max(x[[1]]$coconut #[1] 18 A.K.

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread arun kirshna [via R]
Hello, I was in a hurry at the time, when I replied. You could do one thing: extract sublist using: list4 - list(mango, banana, pineapple) b1-list() for(i in 1:3){ b1[[i]]-list() b1[[i]]-list4[[i]][1] } b1 b-list() for(i in 1:3){ b[[i]]-list()

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread arun kirshna [via R]
Hi Elk, I tried to test with another case where coconut is in different position in the sublist. mango - list( y=c(4,5,3,2,1,8), bw=c(4,18,12,3,4,9), nn=paste0(a,1:6),data.frame(coconut=1:6)) banana - list(data.frame(coconut=c(1,2,18,16,15)), y=c(4,5,9,2,1), bw=c(4,18,22,3,4), nn=paste0(a,1:5))

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread Eik Vettorazzi
Hi Arun, if you name the data.frame in the list, e.g. mango - list( y=c(4,5,3,2,1,8), bw=c(4,18,12,3,4,9), nn=paste0(a,1:6),target=data.frame(coconut=1:6)) banana - list(target=data.frame(coconut=c(1,2,18,16,15)), y=c(4,5,9,2,1), bw=c(4,18,22,3,4), nn=paste0(a,1:5)) pineapple -

Re: [R] apply function over same column of all objects in a list

2012-08-02 Thread arun kirshna [via R]
Thanks Elk, That was the missing link. #or lapply(list6,function(x)max(x$target)) [[1]] [1] 6 [[2]] [1] 18 [[3]] [1] 9 max(unlist(lapply(list6,function(x) max(x$target A.K. ___ If you reply to this email, your message will be added to the

[R] apply function over same column of all objects in a list

2012-08-01 Thread gail
Hello. Please forgive me if this problem has already been posted (and solved) by someone else ... I can't find it anywhere though it seems so very basic. Here it is: I have a list comprised of several matrices, each of which has two columns. list [[1]] [,1] [,2] [1,]1 3 [2,]

Re: [R] apply function over same column of all objects in a list

2012-08-01 Thread gail
Thank you!!! But I realise I've simplified my data to the point that your solution doesn't actually work -- not your fault, mine! My list is actually more complicated than what I presented it to be; it's not composed of numerical matrices but of lists, each being composed of 7 columns, the first

Re: [R] apply function over same column of all objects in a list

2012-08-01 Thread R. Michael Weylandt
On Wed, Aug 1, 2012 at 8:43 AM, gail ortog...@yahoo.de wrote: Hello. Please forgive me if this problem has already been posted (and solved) by someone else ... I can't find it anywhere though it seems so very basic. Here it is: I have a list comprised of several matrices, each of which has

Re: [R] apply function over same column of all objects in a list

2012-08-01 Thread R. Michael Weylandt
I'm a little confused on your data structure -- can you use dput() as described here [1] to give a small reproducible example? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Michael On Wed, Aug 1, 2012 at 10:38 AM, gail ortog...@yahoo.de wrote: Thank

Re: [R] apply function over same column of all objects in a list

2012-08-01 Thread arun kirshna [via R]
HI, I tested the code for another set of data with added complexity. Seems to be working fine. list1-list(data.frame(x=1:6,y=c(4,5,3,2,1,8),bw=c(4,18,12,3,4,9))) list2-list(data.frame(x=c(1,2,18,16,15),y=c(4,5,9,2,1),bw=c(4,18,22,3,4)))

[R] apply() function: margin argument: 2L versus 2

2012-06-22 Thread Luba G
What is the difference of using 2L versus 2 as the margin argument in the apply() function? Where can I find detailed information on all of the possible margin arguments? x [,1] [,2] [1,]12 [2,]34 [3,]56 [4,]78 [5,]9 10 sqrt(apply(x, *2L*,

Re: [R] apply() function: margin argument: 2L versus 2

2012-06-22 Thread Rui Barradas
Hello, 2L is an integer, 2 might be or not. In the case of apply(), there is no difference. All possible values for margin? Any possible(*) combination of the dimensions of 'x' in apply(x, margin, function) (*) non-null. If, say, x - array(0, dim=c(2,3,4)) (3dim) then you can call any of

Re: [R] apply() function: margin argument: 2L versus 2

2012-06-22 Thread R. Michael Weylandt
On Fri, Jun 22, 2012 at 5:53 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, 2L is an integer, 2 might be or not. In the case of apply(), there is no difference. All possible values for margin? Any possible(*) combination of the dimensions of 'x' in apply(x, margin, function) (*)

Re: [R] apply() function: margin argument: 2L versus 2

2012-06-22 Thread Rui Barradas
Right, I had missed that one. To the op: what Michael is saying can be shown with the following example. x - array(1:24, c(2,3,4)) r1 - apply(x, -1, sum) # remove dim 1 r2 - apply(x, 2:3, sum) # include dims 2 and 3 all.equal(r1, r2) # FALSE, different attributes all(r1 == r2) # TRUE,

Re: [R] apply() function: margin argument: 2L versus 2

2012-06-22 Thread Luba G
Thanks Rui! I was not aware that *number*L is an alternate to as.integer(*number*). Luba On Fri, Jun 22, 2012 at 3:53 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, 2L is an integer, 2 might be or not. In the case of apply(), there is no difference. All possible values for margin? Any

Re: [R] apply() function: margin argument: 2L versus 2

2012-06-22 Thread Bert Gunter
FYI below. On Fri, Jun 22, 2012 at 4:00 PM, Luba G lubaglouk...@gmail.com wrote: Thanks Rui! I was not aware that *number*L is an alternate to as.integer(*number*). Luba identical(2,as.integer(2)) [1] FALSE identical(2L,as.integer(2)) [1] TRUE Cheers, Bert On Fri, Jun 22, 2012 at

[R] Apply function to every 'nth' element of a vector

2012-04-05 Thread Michael Bach
Dear R users, how do I e.g. square each second element of a vector with an even number of elements? Or more generally to apply a function to every 'nth' element of a vector. I looked into the apply functions, but found no hint. For example: v - c(1, 2, 3, 4) mysquare - function (x) { return

Re: [R] Apply function to every 'nth' element of a vector

2012-04-05 Thread Ista Zahn
Good morning Michael, On Thu, Apr 5, 2012 at 7:01 AM, Michael Bach pha...@gmail.com wrote: Dear R users, how do I e.g. square each second element of a vector with an even number of elements? Or more generally to apply a function to every 'nth' element of a vector. I looked into the apply

Re: [R] Apply function to every 'nth' element of a vector

2012-04-05 Thread ken knoblauch
Michael Bach phaebz at gmail.com writes: how do I e.g. square each second element of a vector with an even number of elements? Or more generally to apply a function to every 'nth' element of a vector. I looked into the apply functions, but found no hint. For example: v - c(1, 2, 3, 4)

Re: [R] Apply function to every 'nth' element of a vector

2012-04-05 Thread ken knoblauch
ken knoblauch ken.knoblauch at inserm.fr writes: Michael Bach phaebz at gmail.com writes: how do I e.g. square each second element of a vector with an even number of elements? Or more generally to apply a function to every 'nth' element of a vector. I looked into the apply

Re: [R] Apply function to every 'nth' element of a vector

2012-04-05 Thread David Winsemius
On Apr 5, 2012, at 7:01 AM, Michael Bach wrote: Dear R users, how do I e.g. square each second element of a vector with an even number of elements? Or more generally to apply a function to every 'nth' element of a vector. I looked into the apply functions, but found no hint. For example: v

Re: [R] Apply function to every 'nth' element of a vector

2012-04-05 Thread Michael Bach
Thank you very much for your comments Ista and David! I will experiment and see which one serves my needs best. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide

[R] Apply Function to List by Group Returning Result with Dim of List

2012-01-19 Thread jamesrobinsonjnr
I have a panel data set defined as a simple data.frame with a factor age and income. I would like to generate the results of a set function such as (sum, mean, or even diff) separate for each factor (category or age group). However I want the result to have the same dimension as the overal

Re: [R] Apply Function to List by Group Returning Result with Dim of List

2012-01-19 Thread David Winsemius
On Jan 19, 2012, at 6:42 PM, jamesrobinsonjnr wrote: I have a panel data set defined as a simple data.frame with a factor age and income. I would like to generate the results of a set function such as (sum, mean, or even diff) separate for each factor (category or age group). However I

Re: [R] Apply Function to List by Group Returning Result with Dim of List

2012-01-19 Thread jamesrobinsonjnr
Excellent. Thank you very much David. Your help is greatly appreciated. ave worked like a charm. James -- View this message in context: http://r.789695.n4.nabble.com/Apply-Function-to-List-by-Group-Returning-Result-with-Dim-of-List-tp4311880p4312262.html Sent from the R help mailing list

[R] apply function to spatial grid data frame to calculate CTI

2011-08-25 Thread Tom Vanwalleghem
Dear R helpers, I'm new to R and struggling with applying functions to a spatial grid data frame, called data for example.data has a variable named slope and uparea Basically, I need to calculate the tan(data$slope)Next, I need to divide data$uparea by data$slope and take the log() of that all

Re: [R] apply function to spatial grid data frame to calculate CTI

2011-08-25 Thread Jean V Adams
It's not clear to me what problem you're having. tan(data$slope) should work. As should log(data$uparea/data$slope) I suggest that you provide a small subset of example data along with an example of any code you've tried, and the output that you'd like. Jean Tom Vanwalleghem wrote on

Re: [R] apply function to spatial grid data frame to calculate CTI

2011-08-25 Thread Paul Hiemstra
On 08/25/2011 10:54 AM, Tom Vanwalleghem wrote: Dear R helpers, I'm new to R and struggling with applying functions to a spatial grid data frame, called data for example.data has a variable named slope and uparea Basically, I need to calculate the tan(data$slope)Next, I need to divide

[R] apply function returning different results

2011-03-18 Thread Tanya Cashorali
For some reason when I apply a function to a single row in R it works, but when that same row is in a data.frame it does not, see below: apply(dx[954,], 1, query.db) 2571 1092 0.06044147 3890 0.05948577 3273 0.05911261 794 0.05855583 4241 0.05854064 3560 0.05602690

[R] Apply Function To Each Row of Matrix

2011-02-14 Thread Stuart Jaffe
Hi, I have a matrix with about 3000 rows, and 1 column. The contents of the matrix are stock symbols (IBM, AAPL, etc) for instance. I also have a function that takes a single stock symbol as an argument, parses the text of Google Finance and returns the related stock symbols that the page has

[R] Apply function over elemetns of a list

2010-10-02 Thread PQuery
Hello, I have a list ll - see below on which I would like to apply a function accessing every pair of elements in the list. For instance, I want to apply the sum function on 6635 + 6636 and return the sum, the on 6635 + 6637, ... Any hint to do that using apply / mapply / rapply ? Thanks,

Re: [R] Apply function over elemetns of a list

2010-10-02 Thread Uwe Ligges
On 02.10.2010 18:00, PQuery wrote: Hello, I have a list ll - see below on which I would like to apply a function accessing every pair of elements in the list. For instance, I want to apply the sum function on 6635 + 6636 and return the sum, the on 6635 + 6637, ... Any hint to do that using

[R] Apply function to two variables

2010-08-02 Thread Suphajak Ngamlak
Dear all, I would like to apply a function to two vectors For example, A-c(NA,1,2,3,NA) B-6:10 I would like C to be equal to A but if any A element is NA that corresponding c element is equal to B, i.e. C = c(6,1,2,3,10) Please note that this is a simplified example and the real vectors are

Re: [R] Apply function to two variables

2010-08-02 Thread Erik Iverson
I would like to apply a function to two vectors For example, A-c(NA,1,2,3,NA) B-6:10 I would like C to be equal to A but if any A element is NA that corresponding c element is equal to B, i.e. C = c(6,1,2,3,10) #untested... C - ifelse(is.na(A), B, A)

Re: [R] apply function with grouped columns

2010-01-29 Thread Peter Ehlers
Try this: y - c(v1, v2, v3, v4) rowf - gl(2, 1, 16) colf - gl(2, 8, 16, labels=1:0) dat - data.frame(y) aggregate(dat[1], list(Row=rowf, Col=colf), mean) -Peter Ehlers Jack Siegrist wrote: I have a data set of many rows and many columns in which both the rows and the columns have associated

[R] apply function across two variables by mult factors

2009-09-16 Thread Jon Loehrke
Greetings, I am attempting to run a function, which produces a vector and requires two input variables, across two nested factor levels. I can do this using by(X, list(factor1, factor2), function), however I haven't found a simple way to extract the list output into an organized vector

Re: [R] apply function across two variables by mult factors

2009-09-16 Thread Erik Iverson
Hello, -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jon Loehrke Sent: Wednesday, September 16, 2009 2:23 PM To: r-help@r-project.org Subject: [R] apply function across two variables by mult factors Greetings, I am

Re: [R] apply function across two variables by mult factors

2009-09-16 Thread Erik Iverson
One correction below, ---snip--- # example data frame testDF-data.frame( x=rnorm(12), y=rnorm(12), f1=gl(3,4), f2=gl(2,2,12)) Try this using lapply, split, mapply? Maybe it is in a nicer output object for you? testFun2 - function(x, y) { X - abs(x);

Re: [R] apply function across two variables by mult factors

2009-09-16 Thread Jorge Ivan Velez
Hi Jon, Here is a suggestion: foo - function(x) as.numeric( paste( abs( round( x ) ), collapse = . ) ) testDF$value - apply( testDF[,1:2], 1, foo ) testDF HTH, Jorge On Wed, Sep 16, 2009 at 3:22 PM, Jon Loehrke jloeh...@umassd.edu wrote: Greetings, I am attempting to run a function, which

Re: [R] apply function across two variables by mult factors

2009-09-16 Thread Gabor Grothendieck
Try this: transform(testDF, value = as.numeric(paste(round(abs(x)), round(abs(y)), sep = .))) On Wed, Sep 16, 2009 at 3:22 PM, Jon Loehrke jloeh...@umassd.edu wrote: Greetings, I am attempting to run a function, which produces a vector and requires two input variables, across two nested

[R] apply function to named numeric object

2009-08-03 Thread Paulo Eduardo Cardoso
Maybe a very basic question. How can I get mean of a object like this str(object) num [1:53, 1:2] 734 734 736 734 736 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:2] x y I want the mean of both x and y ___ Paulo E. Cardoso [[alternative HTML version

Re: [R] apply function to named numeric object

2009-08-03 Thread David Winsemius
On Aug 3, 2009, at 9:07 AM, Paulo Eduardo Cardoso wrote: Maybe a very basic question. How can I get mean of a object like this str(object) num [1:53, 1:2] 734 734 736 734 736 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:2] x y I want the mean of both x and y apply(object,

Re: [R] apply function to named numeric object

2009-08-03 Thread Paulo Eduardo Cardoso
Thanks. Works A doubt. I asked that because I have a loop that can generate objects like that or this one str(object) Named num [1:2] 670 314 - attr(*, names)= chr [1:2] x y To avoid using a ifelse() to calculate means of x and y, I'd like a function that handle with this named numeric object

Re: [R] apply function to named numeric object

2009-08-03 Thread David Winsemius
On Aug 3, 2009, at 9:52 AM, Paulo Eduardo Cardoso wrote: Thanks. Works A doubt. I asked that because I have a loop that can generate objects like that or this one str(object) Named num [1:2] 670 314 - attr(*, names)= chr [1:2] x y To avoid using a ifelse() to calculate means of x

Re: [R] apply function to named numeric object

2009-08-03 Thread Paulo Eduardo Cardoso
Both objects are numeric, but they differ in structure and I don't know how to handle both with a single function to get means. When I try to apply colMeans to a named numeric object like this: Named num [1:2] 670 314 - attr(*, names)= chr [1:2] x y I get the error messages: Error in

Re: [R] apply function to named numeric object

2009-08-03 Thread David Winsemius
So you need to show us how you made the second object. (The Posting Guide's phrase reproducible code is hereby invoked.) On Aug 3, 2009, at 10:18 AM, Paulo Eduardo Cardoso wrote: Both objects are numeric, but they differ in structure and I don't know how to handle both with a single

Re: [R] apply function to named numeric object

2009-08-03 Thread David Winsemius
That may just be a vector with two elements, so functions designed for matrices are not going to work. Why are you taking means of such small data objects anyway? obj - c(x=640, y=410) obj x y 640 410 str(obj) Named num [1:2] 640 410 - attr(*, names)= chr [1:2] x y mean(obj)

Re: [R] apply function to named numeric object

2009-08-03 Thread Paulo Eduardo Cardoso
The objects are centroid x y 669.5 313.5 str(centroid) Named num [1:2] 670 314 - attr(*, names)= chr [1:2] x y or centroid x y [1,] 659.5 472.5 [2,] 659.5 473.5 str(centroid) num [1:2, 1:2] 660 660 472 474 - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : chr [1:2]

Re: [R] apply function to named numeric object

2009-08-03 Thread ONKELINX, Thierry
] Onderwerp: Re: [R] apply function to named numeric object Both objects are numeric, but they differ in structure and I don't know how to handle both with a single function to get means. When I try to apply colMeans to a named numeric object like this: Named num [1:2] 670 314 - attr(*, names

Re: [R] apply function to named numeric object

2009-08-03 Thread Paulo Eduardo Cardoso
: David Winsemius CC: [R] Onderwerp: Re: [R] apply function to named numeric object Both objects are numeric, but they differ in structure and I don't know how to handle both with a single function to get means. When I try to apply colMeans to a named numeric object like this: Named num [1:2

[R] Apply function

2008-11-11 Thread tedzzx
Hi all I am manipulating a datafram which has 3 variables.Such as: 1 2 3 4 5 6 . . . 200 210 300 I also has a row index: index=c(100,200,250..300) I want to find the sums of first 100 rows, then the sum from row 101 to row 200, then row 201 to row 250... the each end row is indicated

Re: [R] *apply function for arrays?

2008-05-19 Thread Uwe Ligges
Hesen Peng wrote: Hi, Thank you so much for the help. apply does work for the first situation. For the second situation, I'm currently using: temp.a-function(i,j){ return(G(M[i,],N[j,])) } temp.v-Vectorize(temp.a) result-outer(1:nrow(M),1:nrow(N),FUN=temp.v) Looks pretty good! Uwe

Re: [R] *apply function for arrays?

2008-05-18 Thread Uwe Ligges
Hesen Peng wrote: Hi all, I've recently been writing functions which may deal with very large arrays. And I hope to use *apply functions in the program so that the code may look nicer and the performance may be better in the following two situations. The first situation is: I'm having an

Re: [R] *apply function for arrays?

2008-05-18 Thread Hesen Peng
Hi, Thank you so much for the help. apply does work for the first situation. For the second situation, I'm currently using: temp.a-function(i,j){ return(G(M[i,],N[j,])) } temp.v-Vectorize(temp.a) result-outer(1:nrow(M),1:nrow(N),FUN=temp.v) And I wonder whether there are some other ways to

[R] *apply function for arrays?

2008-05-17 Thread Hesen Peng
Hi all, I've recently been writing functions which may deal with very large arrays. And I hope to use *apply functions in the program so that the code may look nicer and the performance may be better in the following two situations. The first situation is: I'm having an array A with

[R] apply function

2008-05-15 Thread Shubha Vishwanath Karanth
Hi R, Getting a strange result using ?apply. Please look into the below codes: d=data.frame(a=c(1,2,3),b=c(A,B,C),c=c(TRUE,FALSE,FALSE),d=c(T,F,F )) class(d[,1]) [1] numeric class(d[,2]) [1] factor class(d[,3]) [1] logical class(d[,4]) [1] logical apply(d,2,class)

Re: [R] apply function

2008-05-15 Thread Berwin A Turlach
G'day Shuba, On Thu, 15 May 2008 12:18:58 +0530 Shubha Vishwanath Karanth [EMAIL PROTECTED] wrote: Getting a strange result using ?apply. Please look into the below codes: d=data.frame(a=c(1,2,3),b=c(A,B,C),c=c(TRUE,FALSE,FALSE),d=c(T,F,F)) class(d[,1]) [1] numeric class(d[,2])