Re: [R] apply

2024-10-04 Thread Richard O'Keefe
> x <- runif(10) > y <- runif(10) > cov(cbind(x,y)) x y x 0.1205034 0.02642830 y 0.0264283 0.09945432 I understand wanting to calculate covariance matrices. What I DON'T understand is wanting to do it using apply(). (And that's what looked like a homework problem, it's so artifi

Re: [R] apply

2024-10-04 Thread Rolf Turner
On Fri, 04 Oct 2024 11:16:45 -0700 Jeff Newmiller via R-help wrote: > Even if this is not a homework question, it smells like one. If you > read the Posting Guide it warns you that homework is off-topic, so > when you impose an arbitrary constraint like "must use specific > unrelated function" we

Re: [R] apply

2024-10-04 Thread Jeff Newmiller via R-help
Even if this is not a homework question, it smells like one. If you read the Posting Guide it warns you that homework is off-topic, so when you impose an arbitrary constraint like "must use specific unrelated function" we feel like you are either cheating or wasting our time, and it is up to you

Re: [R] apply

2024-10-04 Thread Steven Yen
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable

Re: [R] apply

2024-10-04 Thread Rui Barradas via R-help
Hello, You don't need apply, covariance calculations are so frequent that R or any other statistics package already has pre-programmed functions. This time with two vectors x and y. set.seed(123) n <- 3 x <- rnorm(n) y <- rnorm(n) # the two main diagonal values var(x) #> [1] 1.300025 var(y)

Re: [R] apply

2024-10-04 Thread Ebert,Timothy Aaron
Why must the answer use apply? It feels like there are elements of the problem that are not explained. -Original Message- From: R-help On Behalf Of Ben Bolker Sent: Friday, October 4, 2024 8:45 AM To: r-help@r-project.org Subject: Re: [R] apply [External Email] It's still ha

Re: [R] apply

2024-10-04 Thread Ben Bolker
It's still hard to figure out what you want. If you have two vectors you can compute their (2x2) covariance matrix using cov(cbind(x,y)). If you want to compute all pairwise squared differences between elements of x and y you could use outer(x, y, "-")^2. Can you explain a little bit more

Re: [R] apply

2024-10-04 Thread Ivan Krylov via R-help
В Fri, 4 Oct 2024 20:28:01 +0800 Steven Yen пишет: > Suppose I have two vectors, x and y. Is there a way > to do the covariance matrix with “apply”. There is no covariance matrix for just two samples (vectors) 'x' and 'y'. You can only get one covariance value for these. If you had a pair of v

Re: [R] apply

2024-10-04 Thread Steven Yen
OK. Thanks to all. Suppose I have two vectors, x and y. Is there a way to do the covariance matrix with “apply”. The matrix I need really contains the deviation products divided by the degrees of freedom (n-1). That is, the elements (1,1), (1,2),...,(1,n) (2,1), (2,2),, (2,n) (n,1)

Re: [R] apply

2024-10-04 Thread Rui Barradas
Hello, This doesn't make sense, if you have only one vector you can estimate its variance with var(x) but there is no covariance, the joint variance of two rv's. "co" or joint with what if you have only x? Note that the variance of x[1] or any other vector element is zero, it's only one va

Re: [R] apply

2024-10-04 Thread Ivan Krylov via R-help
В Fri, 4 Oct 2024 19:14:30 +0800 Steven Yen пишет: > I have a vector: > set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 > 1.55870831 > var(x[1]) cov(x[1],x[2]) Are you sure you don't have a matrix? If you type var(x[1]) or cov(x[1],x[2]) into R, you can see that all these are

Re: [R] apply

2024-10-04 Thread Steven Yen
Hello I have a vector: set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 1.55870831 I like to create a matrix with elements containing variances and covariances of x. That is var(x[1]) cov(x[1],x[2]) cov(x[1],x[3]) cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) cov(x[3]

Re: [R] apply

2024-10-04 Thread Rui Barradas
Hello, If you have a numeric matrix or data.frame, try something like cov(mtcars) Hope this helps, Rui Barradas Às 10:15 de 04/10/2024, Steven Yen escreveu: On 10/4/2024 5:13 PM, Steven Yen wrote: Pardon me!!! What makes you think this is a homework question? You are not obligated to res

Re: [R] apply

2024-10-04 Thread Uwe Ligges
On 04.10.2024 11:13, Steven Yen wrote: Pardon me!!! What makes you think this is a homework question? You are not obligated Otherwise you called cov() Best, Uwe Ligges to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance

Re: [R] apply

2024-10-04 Thread Steven Yen
On 10/4/2024 5:13 PM, Steven Yen wrote: > Pardon me!!! > > What makes you think this is a homework question? You are not > obligated to respond if the question is not intelligent enough for you. > > I did the following: two ways to calculate a covariance matrix but > wonder how I might replicate

Re: [R] apply

2024-10-04 Thread Uwe Ligges
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote: The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.

[R] apply

2024-10-04 Thread Steven Yen
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/l

Re: [R] apply a function to a list of lists

2023-09-20 Thread Ivan Krylov
On Tue, 19 Sep 2023 17:14:58 +0200 arnaud gaboury wrote: > non_empty_df <- function(l) { > lapply(l, function(df) df[sapply(df, function(df) nrow(df) !=0)]) > } > If I test this way: non_empty_df(my.list[1]) it does the job. It will > return the data.frame from the first list of my_list with

[R] apply a function to a list of lists

2023-09-19 Thread arnaud gaboury
I have a list of 9 lists called my.list. Each one of these 9 lists is itself a list of 6 data.frames. Most of these data.frames have 0 rows and 0 columns. I want to return all data.frames from the list with row numbers different from 0. I first created the following function: non_empty_df <- funct

Re: [R] 'apply' for 0 or 1 element member.

2023-08-10 Thread Jeff Newmiller
Define mean0na <- function(x) { x[ 0 == x ] <- NA mean( x, na.rm = TRUE ) } and then use that instead of mean. On August 10, 2023 7:25:08 PM PDT, ani jaya wrote: >Hello, > >I try to calculate the mean of an array with a condition. My array is >B=c(181,101,420) in dimensions. And I want to f

[R] 'apply' for 0 or 1 element member.

2023-08-10 Thread ani jaya
Hello, I try to calculate the mean of an array with a condition. My array is B=c(181,101,420) in dimensions. And I want to find a specific member in 3rd dimension (time monthly) based on a condition of another data frame, A =c(420). My final array would be C=c(181,101,12) which 12 is a monthly mea

Re: [R] apply to row and column of matrix

2022-09-26 Thread Eric Berger
Bert provided an excellent answer to your question. FYI here is a different approach to do the calculation. It uses data.frame rather than matrix. A data frame is a list of its columns. Here the function supplied to sapply operates on each column of the data.frame. > m <- as.data.frame(t(matrix(1:

Re: [R] apply to row and column of matrix

2022-09-26 Thread Bert Gunter
from ?apply: "If each call to FUN returns a vector of length n, and simplify is TRUE, then apply returns an array of dimension c(n, dim(X)[MARGIN]) ." For margin = 1 (cumsum over rows), each call to cumsum return a vector of length 2. Hence the array returned will be of dimension c(2, c(5,2)[1]) =

[R] apply to row and column of matrix

2022-09-26 Thread Jinsong Zhao
Hi there, I try to calculate the cumsum of row and column of a matrix as follows. > m <- matrix(1:10, ncol = 2) > m [,1] [,2] [1,]    1    6 [2,]    2    7 [3,]    3    8 [4,]    4    9 [5,]    5   10 > apply(m, 1, cumsum) [,1] [,2] [,3] [,4] [,5] [1,]    1    2    3    4    5 [2,]   

Re: [R] "apply" a function that takes two or more vectors as arguments, such as cor(x, y), over a "category" or "grouping variable" or "index"?

2022-04-09 Thread Kelly Thompson
Thanks. I have a clarification and a follow-up question. I should have asked this in the original post, and I should have provided a better example for the FUN argument, I apologize. For use in an example, here is a "silly" example of a function that requires arguments such as x and y to be "separ

Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Luigi Marongiu
Thank you, it works! On Mon, Aug 9, 2021 at 3:26 PM Andrew Simmons wrote: > > Hello, > > > There are two convenient ways to access a column in a data.frame using `$` > and `[[`. Using `df` from your first email, we would do something like > > df <- data.frame(VAR = 1:3, VAL = c("value is blue",

Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Andrew Simmons
Hello, There are two convenient ways to access a column in a data.frame using `$` and `[[`. Using `df` from your first email, we would do something like df <- data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red", "empty")) df$VAL df[["VAL"]] The two convenient ways to update / / replac

Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Luigi Marongiu
I wanted to remove possible white spaces before or after the string. Actually, it worked, I used `gsub("[:blank:]*val[:blank:]*", "", df$VAL, ignore.case=TRUE)`. I don't know why in the example there were extra columns -- they did not came out in the real case. Thank you, I think the case is closed

Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Jim Lemon
Hi Luigi, You want to get rid of certain strings in the "VAL" column. You are assigning to: df[df$VAL] Error in `[.data.frame`(df, df$VAL) : undefined columns selected when I think you should be assigning to: df$VAL What do you want to remove other than "[V|v]alue is" ? JIim On Mon, Aug 9, 20

Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Luigi Marongiu
Sorry, silly question, gsub works already with regex. But still, if I add `[[:blank:]]` still I don't get rid of all instances. And I am keeping obtaining extra columns ``` > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE) > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL

Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Luigi Marongiu
Thank you, that is much appreciated. But on the real data, the substitution works only on few instances. Is there a way to introduce regex into this? Cheers Luigi On Mon, Aug 9, 2021 at 11:01 AM Jim Lemon wrote: > > Hi Luigi, > Ah, now I see: > > df$VAL<-gsub("Value is","",df$VAL,ignore.case=TRU

Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Jim Lemon
Hi Luigi, Ah, now I see: df$VAL<-gsub("Value is","",df$VAL,ignore.case=TRUE) df VAR VAL 1 1 blue 2 2 red 3 3 empty Jim On Mon, Aug 9, 2021 at 6:43 PM Luigi Marongiu wrote: > > Hello, > I have a dataframe where I would like to change the string of certain > rows, essentially I am lo

[R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Luigi Marongiu
Hello, I have a dataframe where I would like to change the string of certain rows, essentially I am looking to remove some useless text from the variables. I tried with: ``` > df = data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red", "empty")) > df[df$VAL] = gsub("value is ", "", df$VAL,

Re: [R] apply a color range (kind of like a heat map) to the values in each cell of a data frame

2021-06-18 Thread Bert Gunter
ing > > -Original Message- > From: Jim Lemon [mailto:drjimle...@gmail.com] > Sent: Thursday, June 17, 2021 8:29 PM > To: Yuan Chun Ding > Cc: r-help@r-project.org > Subject: Re: [R] apply a color range (kind of like a heat map) to the > values in each cell of a data frame

Re: [R] apply a color range (kind of like a heat map) to the values in each cell of a data frame

2021-06-18 Thread Yuan Chun Ding
Ding Cc: r-help@r-project.org Subject: Re: [R] apply a color range (kind of like a heat map) to the values in each cell of a data frame Hi Ding, There are a number of "value to color" functions in various packages. One is "color.scale" in the plotrix package: lib

Re: [R] apply a color range (kind of like a heat map) to the values in each cell of a data frame

2021-06-17 Thread Jim Lemon
Hi Ding, There are a number of "value to color" functions in various packages. One is "color.scale" in the plotrix package: library(plotrix) s1 <-c(0.085,0.086,0.139,0.129,0.235,0.177,0.000,0.126,0.271,0.000,0.083,0.163) s2 <-c(0.000,0.093,0.000,0.080,0.072,0.388,0.138,0.107,0.000,0.000,0.474,0.00

[R] apply a color range (kind of like a heat map) to the values in each cell of a data frame

2021-06-17 Thread Yuan Chun Ding
Dear R users, I have a numeric table with 140 rows and 30 columns, here I only made partial table, test1, as an example. I want to apply a blue color range to the value in each cell of the data frame test1. I found some R code using DT library. However, I only can see the colored table at m

Re: [R] apply and cor()

2017-05-12 Thread Micha Silver
-help [mailto:r-help-boun...@r-project.org] On Behalf Of David L Carlson Sent: Friday, May 12, 2017 10:48 AM To: Ismail SEZEN ; Micha Silver Cc: R-help@r-project.org Subject: Re: [R] apply and cor() Actually, r is a vector, not an index value. You need apply(compare_data, 1, function(r) cor(r, t(test

Re: [R] apply and cor()

2017-05-12 Thread David L Carlson
-project.org Subject: Re: [R] apply and cor() Actually, r is a vector, not an index value. You need apply(compare_data, 1, function(r) cor(r, t(test_data))) - David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-

Re: [R] apply and cor()

2017-05-12 Thread David L Carlson
to:r-help-boun...@r-project.org] On Behalf Of Ismail SEZEN Sent: Friday, May 12, 2017 10:11 AM To: Micha Silver Cc: R-help@r-project.org Subject: Re: [R] apply and cor() > On 12 May 2017, at 17:57, Micha Silver wrote: > > I have two data.frames, one with a single row of 31 columns, a

Re: [R] apply and cor()

2017-05-12 Thread Ismail SEZEN
> On 12 May 2017, at 17:57, Micha Silver wrote: > > I have two data.frames, one with a single row of 31 columns, and the second > with 269 rows and the same 31 columns. > > dim(compare_data) > [1] 269 31 > > dim(test_data) > [1] 1 31 > > I want to apply cor() between the one row of 'test_dat

[R] apply and cor()

2017-05-12 Thread Micha Silver
I have two data.frames, one with a single row of 31 columns, and the second with 269 rows and the same 31 columns. > dim(compare_data) [1] 269 31 > dim(test_data) [1] 1 31 I want to apply cor() between the one row of 'test_data', and each row of the 'compare_data' . I tried 'apply' but I get

Re: [R] apply weight to a data frame

2016-09-16 Thread PIKAL Petr
these functions could result in object suitable for plm Cheers Petr From: laura roncaglia [mailto:roncaglia.la...@gmail.com] Sent: Friday, September 16, 2016 9:36 AM To: PIKAL Petr Subject: Re: [R] apply weight to a data frame Thank you for your answer. The data frame contains social and economic

Re: [R] apply weight to a data frame

2016-09-16 Thread PIKAL Petr
r-help-boun...@r-project.org] On Behalf Of laura > roncaglia > Sent: Thursday, September 15, 2016 6:40 PM > To: r-help@r-project.org > Subject: [R] apply weight to a data frame > > I am a beginner user of R. > > I am writing the master thesis using a data frame from a national s

[R] apply weight to a data frame

2016-09-15 Thread laura roncaglia
I am a beginner user of R. I am writing the master thesis using a data frame from a national survey. The data frame contains several variables, one of which contains the survey weights. I need to apply the survey weights to the data frame, in order to use the data frame with the plm package (I ne

Re: [R] Apply a multi-variable function to a vector

2016-09-13 Thread S Ellison
> I would like to define an arbitrary function of an arbitrary number of > variables, > for example, for 2 variables: > > func2 <- function(time, temp) time + temp > > I'd like to keep variable names that have a meaning in the problem (time and > temperature above). Not quite enough information

Re: [R] Apply a multi-variable function to a vector

2016-09-12 Thread Stephen Kennedy
c2, >> as.list(a))) >> >> although this does work: >> >> do.call(func2, as.list(c(10, 121))) >> >> And, this also works: >> >> apply(expand.grid(temps,times), 1, function(a) do.call("+", as.list(a))) >> >> There

Re: [R] Apply a multi-variable function to a vector

2016-09-10 Thread Stephen Kennedy
= c(1:5), this doesn't quite seem to work: >> >> apply(expand.grid(temps,times), 1, function(a) do.call(func2, >> as.list(a))) >> >> although this does work: >> >> do.call(func2, as.list(c(10, 121))) >> >> And, this also works: >> >

Re: [R] Apply a multi-variable function to a vector

2016-09-09 Thread Jeff Newmiller
iginal Message- From: Jeff Newmiller [mailto:jdnew...@dcn.davis.ca.us] Sent: Friday, September 09, 2016 5:39 PM To: Steve Kennedy; r-help@r-project.org Subject: Re: [R] Apply a multi-variable function to a vector Your architecture has a bad smell to me. For one thing you are mixing different units

Re: [R] Apply a multi-variable function to a vector

2016-09-09 Thread Jeff Newmiller
Your architecture has a bad smell to me. For one thing you are mixing different units in the same vector but should be putting multiple instances of the same variable into one vector. Lists of vectors (data frames) are typically used when multiple variables need to be grouped. Another problem i

Re: [R] Apply a multi-variable function to a vector

2016-09-09 Thread William Dunlap via R-help
Try do.call(), as in > func2 <- function(time, temp) paste(time, temp) > func2(121, 10) [1] "121 10" > do.call(func2, as.list(c(121,10))) [1] "121 10" > do.call(func2, list(121,10)) [1] "121 10" > > func2(121, time=10:12) [1] "10 121" "11 121" "12 121" > do.call(func2, list(121,time=10:12)) [1] "1

[R] Apply a multi-variable function to a vector

2016-09-09 Thread Stephen Kennedy
Hello, I would like to define an arbitrary function of an arbitrary number of variables, for example, for 2 variables: func2 <- function(time, temp) time + temp I'd like to keep variable names that have a meaning in the problem (time and temperature above). If I have a vector of values

[R] Apply a multi-variable function to a vector

2016-09-09 Thread Steve Kennedy
Hello, I would like to define an arbitrary function of an arbitrary number of variables, for example, for 2 variables: func2 <- function(time, temp) time + temp I'd like to keep variable names that have a meaning in the problem (time and temperature above). If I have a vector of values for th

Re: [R] apply and cousins

2016-06-09 Thread John Logsdon
Thanks Jim and others (and sorry Jim - an early version of this slipped into your inbox :)) Apologies for not giving some concrete code - I was trying to explain in words. What I need to do is to fit a simple linear model to successive sections of a long matrix. So far, the best solution I have

Re: [R] apply and cousins

2016-06-08 Thread Jim Lemon
Hi John, With due respect to the other respondents, here is something that might help: # get a vector of values foo<-rnorm(100) # get a vector of increasing indices (aka your "recent" values) bar<-sort(sample(1:100,40)) # write a function to "clump" the adjacent index values clump_adj_int<-functio

Re: [R] apply and cousins

2016-06-08 Thread MacQueen, Don
Hopefully Bert and William won't be offended if I more or less summarize: Are you assuming a loop will take ages, or have you actually tested it? I wouldn't assume a loop will take ages, or that it will take much longer than apply(). What's wrong with apply( X[ {logical expression } , ] , 1, f

Re: [R] apply and cousins

2016-06-08 Thread Bert Gunter
John: 1. Please read and follow the posting guide. In particular, provide a small reproducible example so that we know what your data and looping code look like. 2. apply-type commands are *not* vectorized; they are disguised loops that may or may not offer any speedup over explicit loops. 3. A

Re: [R] apply and cousins

2016-06-08 Thread William Dunlap via R-help
>It is easy in a loop but that will take ages. Is there any vectorised >apply-like solution to this? If you showed the loop that takes ages, along with small inputs for it (and an indication of how to expand those small inputs to big ones), someone might be able to show you some code that does the

[R] apply and cousins

2016-06-08 Thread John Logsdon
Folks Is there any way to get the row index into apply as a variable? I want a function to do some sums on a small subset of some very long vectors, rolling through the whole vectors. apply(X,1,function {do something}, other arguments) seems to be the way to do it. The subset I want is the mos

Re: [R] apply formula over columns by subset of rows in a dataframe (to get a new dataframe)

2016-05-14 Thread Massimo Bressan
;David L Carlson" , "r-help" Inviato: Venerdì, 13 maggio 2016 19:22:21 Oggetto: Re: [R] apply formula over columns by subset of rows in a dataframe (to get a new dataframe) ave() encapsulates the split/lapply/unsplit stuff so transform(mydf, v1.mod = ave(v1, blocks, FUN=mynorm

Re: [R] apply formula over columns by subset of rows in a dataframe (to get a new dataframe)

2016-05-13 Thread William Dunlap via R-help
ave() encapsulates the split/lapply/unsplit stuff so transform(mydf, v1.mod = ave(v1, blocks, FUN=mynorm)) also gives what you got above. Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, May 13, 2016 at 7:44 AM, Massimo Bressan < massimo.bres...@arpa.veneto.it> wrote: > yes, thanks > > yo

Re: [R] apply formula over columns by subset of rows in a dataframe (to get a new dataframe)

2016-05-13 Thread Massimo Bressan
yes, thanks you pointed me in the right direction: split/unplist was the trick I completely left behind that possibility! here the final version mynorm <- function(x) {(x - min(x, na.rm=TRUE))/(max(x, na.rm=TRUE) - min(x, na.rm=TRUE))} mydf<-data.frame(blocks=rep(c("a","b","c"

Re: [R] apply formula over columns by subset of rows in a dataframe (to get a new dataframe)

2016-05-13 Thread David L Carlson
ocks) > str(mydf2) 'data.frame': 15 obs. of 4 variables: $ blocks: Factor w/ 3 levels "a","b","c": 1 1 1 1 1 2 2 2 2 2 ... $ v1: num 19 15 17 22 16 12 24 25 22 18 ... $ v2: num 35 31 35 31 39 31 19 35 32 38 ... $ v1mod : num 0.571 0 0.286 1 0.143 ... -----

[R] apply formula over columns by subset of rows in a dataframe (to get a new dataframe)

2016-05-13 Thread Massimo Bressan
hi I need to apply a user defined formula over some selected columns of a dataframe by subsetting group of rows (blocks) and get back a new dataframe I’ve been managed to get the the calculations right but I’m not satisfied at all by the form of the results please refer to my reproducible e

Re: [R] apply mean function to a subset of data

2016-04-04 Thread Pedro Mardones
.117015 > > -- > David L. Carlson > Department of Anthropology > Texas A&M University > > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jim Lemon > Sent: Saturday, April 2, 2016 6:14 PM > To: Pedro Mar

Re: [R] apply mean function to a subset of data

2016-04-03 Thread David L Carlson
2016 6:14 PM To: Pedro Mardones Cc: r-help mailing list Subject: Re: [R] apply mean function to a subset of data Hi Pedro, This may not be much of an improvement, but it was a challenge. selvec<-as.vector(matrix(c(nsel,unlist(by(toy$diam,toy$group,length))-nsel), ncol=2,byrow=TRUE)) TFv

Re: [R] apply mean function to a subset of data

2016-04-02 Thread Jim Lemon
Hi Pedro, This may not be much of an improvement, but it was a challenge. selvec<-as.vector(matrix(c(nsel,unlist(by(toy$diam,toy$group,length))-nsel), ncol=2,byrow=TRUE)) TFvec<-rep(c(TRUE,FALSE),length.out=length(selvec)) toynsel<-rep(TFvec,selvec) by(toy[toynsel,]$diam,toy[toynsel,]$group,mean)

Re: [R] apply mean function to a subset of data

2016-04-02 Thread Boris Steipe
Your toy code does not reproduce what you describe: mean(toy$diam[1:nsel[i]]) both times selects from elements of group 1. YOu probably want to subset like toy$diam[toy$group == i]. Also, if there is any real inefficiency here, it is _not_ because you are executing a for-loop for two iterations.

[R] apply mean function to a subset of data

2016-04-02 Thread Pedro Mardones
Dear all; This must have a rather simple answer but haven't been able to figure it out: I have a data frame with say 2 groups (group 1 & 2). I want to select from group 1 say "n" rows and calculate the mean; then select "m" rows from group 2 and calculate the mean as well. So far I've been using a

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 ) -

[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 g

Re: [R] apply regression to an array

2015-10-06 Thread David Winsemius
On Oct 6, 2015, at 10:42 AM, Adrienne Wootten wrote: > R-Helpers, > > I've seen some similar threads about this question online, but not quite > what I'm looking for. I apologize in advance if someone's already answered > this and I just can't find it online. > > Say that I have an array like

Re: [R] apply regression to an array

2015-10-06 Thread Adrienne Wootten
Bill, Thanks a bunch that works great! A On Tue, Oct 6, 2015 at 2:56 PM, William Dunlap wrote: > Since the model matrix, cbind(1,time) is the same for all your > response variables, > you can calculate this on one call to lm, but you have to rearrange the > response > values so that each x,y s

Re: [R] apply regression to an array

2015-10-06 Thread William Dunlap
Since the model matrix, cbind(1,time) is the same for all your response variables, you can calculate this on one call to lm, but you have to rearrange the response values so that each x,y set is in one column. I think the following function does it: f <- function (time, y) { stopifnot(length(

Re: [R] apply regression to an array

2015-10-06 Thread Adrienne Wootten
Almost forgot that function lmfunc is this: lmfunc = function(valist,input){ fitted.values(lm(valist~input)) } A On Tue, Oct 6, 2015 at 2:41 PM, Adrienne Wootten wrote: > FYI I did try something like this: > > test = apply(test3,c(1,2),lmfunc,input=t) > > but that gives me an array that is

Re: [R] apply regression to an array

2015-10-06 Thread Adrienne Wootten
FYI I did try something like this: test = apply(test3,c(1,2),lmfunc,input=t) but that gives me an array that is 10 rows by 5 columns by 5 slices, and I need it to keep the same dimensions as test3 (5x5x10) A On Tue, Oct 6, 2015 at 1:42 PM, Adrienne Wootten wrote: > R-Helpers, > > I've seen so

[R] apply regression to an array

2015-10-06 Thread Adrienne Wootten
R-Helpers, I've seen some similar threads about this question online, but not quite what I'm looking for. I apologize in advance if someone's already answered this and I just can't find it online. Say that I have an array like test3 in the little example code I have below: test1 = array(rep(1:1

[R] apply with multiple references and database interactivity

2015-08-15 Thread Steve E.
Hi R Colleagues, I have a small R script that relies on two for-loops to pull data from a database, make some edits to the data returned from the query, then inserts the updated data back into the database. The script works just fine, no problems, except that I am striving to get away from loops,

Re: [R] apply kendall tau to a split data set

2015-07-25 Thread John Kane
ON Canada > -Original Message- > From: shah_su...@hotmail.co.uk > Sent: Fri, 24 Jul 2015 04:08:08 -0700 (PDT) > To: r-help@r-project.org > Subject: [R] apply kendall tau to a split data set > > $`19179222` >Unique.IDStart.YearL1.Risk.Cat

[R] apply kendall tau to a split data set

2015-07-24 Thread SRS
$`19179222` Unique.IDStart.YearL1.Risk.Category Gross.amount.sum 17 19179222 2013 Execution, Delivery & Process Management 161212.1 18 19179222 2015 Execution, Delivery & Process Management 110880.0 $`25182498` Uniqu

Re: [R] apply a function to a list of data frames

2015-05-26 Thread Stefano Sofia
Thank you for your help. Your explanations have been very useful. Stefano Da: Rui Barradas [ruipbarra...@sapo.pt] Inviato: venerdì 22 maggio 2015 20.26 A: Stefano Sofia; r-help@r-project.org Oggetto: Re: [R] apply a function to a list of data frames

Re: [R] apply a function to a list of data frames

2015-05-22 Thread Rui Barradas
Hello, You should change your function to accept only one argument, the data.frames, and then use lapply (not sapply). Something like the following. calc <- function(dat) { bias_dmo_max <- round(mean((dat$dmo_12-dat$Eonestep_12), na.rm=TRUE), digits=2) rmse_dmo_max <- round(sqrt(mean((dat$d

Re: [R] apply a function to a list of data frames

2015-05-22 Thread Bert Gunter
Where is your code? I see no invocation of sapply. -- Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll On Fri, May 22, 2015 at 11:02 AM, Stefano Sofia wrote: > D

[R] apply a function to a list of data frames

2015-05-22 Thread Stefano Sofia
Dear R-users, given a list of dataframes (like below reported), for each month I need to apply a function (called calc). The result should be written in a new list of data frames, one row for each month. I have been trying to use sapply, with no success. Could somebody help me in this? $df1 da

Re: [R] Apply t-test on list in R

2015-02-25 Thread Zilefac Elvis via R-help
Many thanks, Petr.You solved my problem.AT. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-proje

Re: [R] Apply t-test on list in R

2015-02-25 Thread PIKAL Petr
ject.org] On Behalf Of Zilefac > Elvis via R-help > Sent: Wednesday, February 18, 2015 4:46 AM > To: R. Help > Subject: [R] Apply t-test on list in R > > I have a list object in R with dataframe names as: > > > "pav_DJF_histo.csv"&qu

[R] Apply t-test on list in R

2015-02-17 Thread Zilefac Elvis via R-help
I have a list object in R with dataframe names as: "pav_DJF_histo.csv""pav_DJF_rcp26_2040s.csv" "pav_DJF_rcp26_2080s.csv" "pav_DJF_rcp45_2040s.csv""pav_DJF_rcp45_2080s.csv" "pav_DJF_rcp85_2040s.csv" "pav_DJF_rcp85_2080s.csv" "pav_JJA_histo.csv" "pav_JJA_rcp26_2040s.csv" "pav_JJ

Re: [R] apply two functions to column

2015-02-12 Thread Jeff Newmiller
You don't need to do these operations in pieces so the mapply is unnecessary. Neither dplyr nor data.table can go faster than (assuming your data frame is called DF): DF$dtm <- as.POSIXct( as.numeric( DF$ts ), tz="GMT", origin="1970-01-01" ) They can in this case save you from having to retype

[R] apply two functions to column

2015-02-12 Thread arnaud gaboury
I am little lost between all the possibilities to apply a function to a data.frame or data.table. Here is mine: structure(list(name = c("poisonivy", "poisonivy", "poisonivy", "poisonivy", "poisonivy", "poisonivy", "poisonivy", "poisonivy", "cruzecontrol", "agreenmamba", "agreenmamba", "vairis", "

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.

Re: [R] Apply Function to Columns

2014-10-24 Thread Sarah
> Le 24 oct. 2014 à 09:23, Sarah 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: > > ddeg.correlog.li

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 help

[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 mi

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 param

[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 if else statement to vector

2014-10-02 Thread Berend Hasselman
On 02-10-2014, at 11:01, r...@openmailbox.org wrote: > Subscribers, > > What is the correct syntax to apply the 'if else' conditional statement to > vector objects? > > Example: > > vectorx<-c(50,50,20,70) > vectory<-c(50,50,20,20) > vectorz<-function () { > if (vectorx>vectory) >

Re: [R] apply if else statement to vector

2014-10-02 Thread PIKAL Petr
ct.org] On Behalf Of r...@openmailbox.org > Sent: Thursday, October 02, 2014 11:02 AM > To: r-help@r-project.org > Subject: [R] apply if else statement to vector > > Subscribers, > > What is the correct syntax to apply the 'if else' conditional statement > to

[R] apply if else statement to vector

2014-10-02 Thread rl
Subscribers, What is the correct syntax to apply the 'if else' conditional statement to vector objects? Example: vectorx<-c(50,50,20,70) vectory<-c(50,50,20,20) vectorz<-function () { if (vectorx>vectory) vectorx else vectorx<-0 } vectorz() Warning message: In if (vec

Re: [R] apply block of if statements with menu function

2014-09-22 Thread PIKAL Petr
Hi > -Original Message- > From: r...@openmailbox.org [mailto:r...@openmailbox.org] > Sent: Thursday, September 18, 2014 4:35 PM > To: PIKAL Petr > Cc: r-help@r-project.org > Subject: RE: [R] apply block of if statements with menu function > > On 2014-09-16

Re: [R] apply block of if statements with menu function

2014-09-18 Thread rl
On 2014-09-16 12:35, PIKAL Petr wrote: So if result of menu is 0 (you did not choose anything) you can either stay with 0, then switch does not return anything or add 1 and let evaluate something meaningful specified in second and following positions of switch command. Thanks for your explana

  1   2   3   4   5   6   >