Curtis Burkhalter
2015-Mar-05 20:41 UTC
[R] problem with function that adds rows to dataframe based on conditional statement
Hello everyone, I'm having a problem with a function that I wrote that is supposed to add a row to dataframe based upon a conditional statement. To explain I've used an example below: #create data frame animals=c("bird","dog","cat") animals=rep(animals,each=4) animals=animals[1:11] animalYears=c(1,1,2,2,1,1,2,2,1,1,2) animalMass=round(runif(11,min=10,max=50),0) comAn=as.data.frame(cbind(animals,animalYears,animalMass)) comAn * animals* *animalYears* *animalMass* 1 bird 1 30 2 bird 1 32 3 bird 2 27 4 bird 2 16 5 dog 1 22 6 dog 1 25 7 dog 2 41 8 dog 2 22 9 cat 1 30 10 cat 1 37 11 cat 2 49 We can see here that for every type of animal I have two years of mass measurements, except for the cat in year 2. What I want to do is add an additional row to the end of the dataframe that consists strictly of NAs and then I can substitute those out later. So what I first did was split the 'comAn' dataframe into the different Animal by Year combos. #This line splits 'com_An' into a list ordered by the Animal by Year combos comAn_split=split(comAn, paste(comAn$animals,comAn$animalYear)) Then I wrote the function that identifies whether a particular Animal by Year combo is less than two rows in length and if so it should add another row that consists only of NAs using the vector 'NAs': #This function identifies the length of each Animal by Year combo and then #uses the rbind function built in R to add a row #to each animal by year combo if they have less than 2 samples addNA <- function(comAn) { NAs=c(NA,NA,NA) ind <- seq_len(nrow(comAn)) comAn[ifelse(length(ind)<2,rbind(NAs),length(ind)),] } #This applies the function addNs to the animals data organized in list format addedNAcomAn <- do.call(rbind, lapply(comAn_split, addNA)) addedNAcomAn When I apply the function to the list of the different Animal by Year combos this is what I get: animals animalYears animalMass bird 1 bird 1 23 bird 2 bird 2 50 cat 1 cat 1 15 cat 2 <NA> <NA> <NA> dog 1 dog 1 23 dog 2 dog 2 38 What I expect is this: animals animalYears animalMass 1 bird 1 41 2 bird 1 23 3 bird 2 23 4 bird 2 50 5 dog 1 49 6 dog 1 23 7 dog 2 13 8 dog 2 38 9 cat 1 42 10 cat 1 15 11 cat 2 33 12 NA NA NA Am I conditioning improperly within the function or am I missing something else. Any help would be greatly appreciated. Best -- Curtis Burkhalter [[alternative HTML version deleted]]
JS Huang
2015-Mar-05 23:29 UTC
[R] problem with function that adds rows to dataframe based on conditional statement
Hi Curtis, Maybe you forgot to tell us how the function seq_len is defined. ----- JS Huang -- View this message in context: http://r.789695.n4.nabble.com/problem-with-function-that-adds-rows-to-dataframe-based-on-conditional-statement-tp4704228p4704237.html Sent from the R help mailing list archive at Nabble.com.
Sarah Goslee
2015-Mar-06 04:24 UTC
[R] problem with function that adds rows to dataframe based on conditional statement
Why should he? seq_len() is part of base R. On Thursday, March 5, 2015, JS Huang <js.huang at protective.com> wrote:> Hi Curtis, > > Maybe you forgot to tell us how the function seq_len is defined. > > > > ----- > JS Huang > -- > View this message in context: > http://r.789695.n4.nabble.com/problem-with-function-that-adds-rows-to-dataframe-based-on-conditional-statement-tp4704228p4704237.html > Sent from the R help mailing list archive at Nabble.com. >Nabble is not either the R help mailing list archive, and if you don't include context in your post the real R help list will have little idea what you're talking about. Sarah -- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org [[alternative HTML version deleted]]
PIKAL Petr
2015-Mar-06 10:04 UTC
[R] problem with function that adds rows to dataframe based on conditional statement
Hi May I ask you why do you need such operation. I cannot imagine a situation where I would need to do this. The only imaginable procedure is to merge NA enhanced object with another object but for that situation usually ?merge is used Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Curtis > Burkhalter > Sent: Thursday, March 05, 2015 9:42 PM > To: r-help at r-project.org > Subject: [R] problem with function that adds rows to dataframe based on > conditional statement > > Hello everyone, > > I'm having a problem with a function that I wrote that is supposed to > add a > row to dataframe based upon a conditional statement. To explain I've > used > an example below: > > #create data frame > animals=c("bird","dog","cat") > animals=rep(animals,each=4) > animals=animals[1:11] > animalYears=c(1,1,2,2,1,1,2,2,1,1,2) > animalMass=round(runif(11,min=10,max=50),0) > > comAn=as.data.frame(cbind(animals,animalYears,animalMass)) > comAn > > * animals* *animalYears* *animalMass* > 1 bird 1 30 > 2 bird 1 32 > 3 bird 2 27 > 4 bird 2 16 > 5 dog 1 22 > 6 dog 1 25 > 7 dog 2 41 > 8 dog 2 22 > 9 cat 1 30 > 10 cat 1 37 > 11 cat 2 49 > > We can see here that for every type of animal I have two years of mass > measurements, except for the cat in year 2. What I want to do is add an > additional row to the end of the dataframe that consists strictly of > NAs > and then I can substitute those out later. > > So what I first did was split the 'comAn' dataframe into the different > Animal by Year combos. > > #This line splits 'com_An' into a list ordered by the Animal by Year > combos > comAn_split=split(comAn, paste(comAn$animals,comAn$animalYear)) > > Then I wrote the function that identifies whether a particular Animal > by > Year combo is less than two rows in length and if so it should add > another > row that consists only of NAs using the vector 'NAs': > > #This function identifies the length of each Animal by Year combo and > then > #uses the rbind function built in R to add a row > #to each animal by year combo if they have less than 2 samples > > addNA <- function(comAn) { > NAs=c(NA,NA,NA) > ind <- seq_len(nrow(comAn)) > comAn[ifelse(length(ind)<2,rbind(NAs),length(ind)),] > } > > #This applies the function addNs to the animals data organized in list > format > addedNAcomAn <- do.call(rbind, lapply(comAn_split, addNA)) > addedNAcomAn > > When I apply the function to the list of the different Animal by Year > combos this is what I get: > animals animalYears animalMass > bird 1 bird 1 23 > bird 2 bird 2 50 > cat 1 cat 1 15 > cat 2 <NA> <NA> <NA> > dog 1 dog 1 23 > dog 2 dog 2 38 > > What I expect is this: > > animals animalYears animalMass > 1 bird 1 41 > 2 bird 1 23 > 3 bird 2 23 > 4 bird 2 50 > 5 dog 1 49 > 6 dog 1 23 > 7 dog 2 13 > 8 dog 2 38 > 9 cat 1 42 > 10 cat 1 15 > 11 cat 2 33 > 12 NA NA NA > > Am I conditioning improperly within the function or am I missing > something > else. Any help would be greatly appreciated. > > Best > > -- > Curtis Burkhalter > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
Curtis Burkhalter
2015-Mar-06 17:27 UTC
[R] problem with function that adds rows to dataframe based on conditional statement
Petr, The reason I need to do this is that I'm going to be using some large datasets in WinBUGS, but there is some missing data for the files I'm using. In order to make the data files readable in WinBUGS there have to be NAs in place of missing data so I'm trying to add in NAs where there are missing sampling occasions for a site in a given year. I tried using 'merge' within my function, but that didn't seem to work either. Thanks for the suggestion, but I've got to try something else. Best On Fri, Mar 6, 2015 at 3:04 AM, PIKAL Petr <petr.pikal at precheza.cz> wrote:> Hi > > May I ask you why do you need such operation. I cannot imagine a situation > where I would need to do this. The only imaginable procedure is to merge NA > enhanced object with another object but for that situation usually > > ?merge > > is used > > Cheers > Petr > > > -----Original Message----- > > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Curtis > > Burkhalter > > Sent: Thursday, March 05, 2015 9:42 PM > > To: r-help at r-project.org > > Subject: [R] problem with function that adds rows to dataframe based on > > conditional statement > > > > Hello everyone, > > > > I'm having a problem with a function that I wrote that is supposed to > > add a > > row to dataframe based upon a conditional statement. To explain I've > > used > > an example below: > > > > #create data frame > > animals=c("bird","dog","cat") > > animals=rep(animals,each=4) > > animals=animals[1:11] > > animalYears=c(1,1,2,2,1,1,2,2,1,1,2) > > animalMass=round(runif(11,min=10,max=50),0) > > > > comAn=as.data.frame(cbind(animals,animalYears,animalMass)) > > comAn > > > > * animals* *animalYears* *animalMass* > > 1 bird 1 30 > > 2 bird 1 32 > > 3 bird 2 27 > > 4 bird 2 16 > > 5 dog 1 22 > > 6 dog 1 25 > > 7 dog 2 41 > > 8 dog 2 22 > > 9 cat 1 30 > > 10 cat 1 37 > > 11 cat 2 49 > > > > We can see here that for every type of animal I have two years of mass > > measurements, except for the cat in year 2. What I want to do is add an > > additional row to the end of the dataframe that consists strictly of > > NAs > > and then I can substitute those out later. > > > > So what I first did was split the 'comAn' dataframe into the different > > Animal by Year combos. > > > > #This line splits 'com_An' into a list ordered by the Animal by Year > > combos > > comAn_split=split(comAn, paste(comAn$animals,comAn$animalYear)) > > > > Then I wrote the function that identifies whether a particular Animal > > by > > Year combo is less than two rows in length and if so it should add > > another > > row that consists only of NAs using the vector 'NAs': > > > > #This function identifies the length of each Animal by Year combo and > > then > > #uses the rbind function built in R to add a row > > #to each animal by year combo if they have less than 2 samples > > > > addNA <- function(comAn) { > > NAs=c(NA,NA,NA) > > ind <- seq_len(nrow(comAn)) > > comAn[ifelse(length(ind)<2,rbind(NAs),length(ind)),] > > } > > > > #This applies the function addNs to the animals data organized in list > > format > > addedNAcomAn <- do.call(rbind, lapply(comAn_split, addNA)) > > addedNAcomAn > > > > When I apply the function to the list of the different Animal by Year > > combos this is what I get: > > animals animalYears animalMass > > bird 1 bird 1 23 > > bird 2 bird 2 50 > > cat 1 cat 1 15 > > cat 2 <NA> <NA> <NA> > > dog 1 dog 1 23 > > dog 2 dog 2 38 > > > > What I expect is this: > > > > animals animalYears animalMass > > 1 bird 1 41 > > 2 bird 1 23 > > 3 bird 2 23 > > 4 bird 2 50 > > 5 dog 1 49 > > 6 dog 1 23 > > 7 dog 2 13 > > 8 dog 2 38 > > 9 cat 1 42 > > 10 cat 1 15 > > 11 cat 2 33 > > 12 NA NA NA > > > > Am I conditioning improperly within the function or am I missing > > something > > else. Any help would be greatly appreciated. > > > > Best > > > > -- > > Curtis Burkhalter > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at 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-project.org/posting- > > guide.html > > and provide commented, minimal, self-contained, reproducible code. > > ________________________________ > Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou > ur?eny pouze jeho adres?t?m. > Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? > neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie > vyma?te ze sv?ho syst?mu. > Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email > jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. > Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi > ?i zpo?d?n?m p?enosu e-mailu. > > V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: > - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? > smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. > - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; > Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany > p??jemce s dodatkem ?i odchylkou. > - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve > v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. > - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za > spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n > nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto > emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich > existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. > > This e-mail and any documents attached to it may be confidential and are > intended only for its intended recipients. > If you received this e-mail by mistake, please immediately inform its > sender. Delete the contents of this e-mail with all attachments and its > copies from your system. > If you are not the intended recipient of this e-mail, you are not > authorized to use, disseminate, copy or disclose this e-mail in any manner. > The sender of this e-mail shall not be liable for any possible damage > caused by modifications of the e-mail or by delay with transfer of the > email. > > In case that this e-mail forms part of business dealings: > - the sender reserves the right to end negotiations about entering into a > contract in any time, for any reason, and without stating any reasoning. > - if the e-mail contains an offer, the recipient is entitled to > immediately accept such offer; The sender of this e-mail (offer) excludes > any acceptance of the offer on the part of the recipient containing any > amendment or variation. > - the sender insists on that the respective contract is concluded only > upon an express mutual agreement on all its aspects. > - the sender of this e-mail informs that he/she is not authorized to enter > into any contracts on behalf of the company except for cases in which > he/she is expressly authorized to do so in writing, and such authorization > or power of attorney is submitted to the recipient or the person > represented by the recipient, or the existence of such authorization is > known to the recipient of the person represented by the recipient. >-- Curtis Burkhalter https://sites.google.com/site/curtisburkhalter/ [[alternative HTML version deleted]]
Tom Wright
2015-Mar-06 20:48 UTC
[R] problem with function that adds rows to dataframe based on conditional statement
If all you want is to add a row of na's could you just do something like: nExpectedRows<-length(unique(animals)) * length(unique(animalYears)) * 2 newDf<-data.frame(animals=rep(NA,nExpectedRows-nrow(comAn)), animalYears=rep(NA,nExpectedRows-nrow(comAn)), animalMass=rep(NA,nExpectedRows-nrow(comAn))) comAn = rbind(comAn,newDf) On Thu, 2015-03-05 at 13:41 -0700, Curtis Burkhalter wrote:> Hello everyone, > > I'm having a problem with a function that I wrote that is supposed to add a > row to dataframe based upon a conditional statement. To explain I've used > an example below: > > #create data frame > animals=c("bird","dog","cat") > animals=rep(animals,each=4) > animals=animals[1:11] > animalYears=c(1,1,2,2,1,1,2,2,1,1,2) > animalMass=round(runif(11,min=10,max=50),0) > > comAn=as.data.frame(cbind(animals,animalYears,animalMass)) > comAn > > * animals* *animalYears* *animalMass* > 1 bird 1 30 > 2 bird 1 32 > 3 bird 2 27 > 4 bird 2 16 > 5 dog 1 22 > 6 dog 1 25 > 7 dog 2 41 > 8 dog 2 22 > 9 cat 1 30 > 10 cat 1 37 > 11 cat 2 49 > > We can see here that for every type of animal I have two years of mass > measurements, except for the cat in year 2. What I want to do is add an > additional row to the end of the dataframe that consists strictly of NAs > and then I can substitute those out later. > > So what I first did was split the 'comAn' dataframe into the different > Animal by Year combos. > > #This line splits 'com_An' into a list ordered by the Animal by Year combos > comAn_split=split(comAn, paste(comAn$animals,comAn$animalYear)) > > Then I wrote the function that identifies whether a particular Animal by > Year combo is less than two rows in length and if so it should add another > row that consists only of NAs using the vector 'NAs': > > #This function identifies the length of each Animal by Year combo and then > #uses the rbind function built in R to add a row > #to each animal by year combo if they have less than 2 samples > > addNA <- function(comAn) { > NAs=c(NA,NA,NA) > ind <- seq_len(nrow(comAn)) > comAn[ifelse(length(ind)<2,rbind(NAs),length(ind)),] > } > > #This applies the function addNs to the animals data organized in list > format > addedNAcomAn <- do.call(rbind, lapply(comAn_split, addNA)) > addedNAcomAn > > When I apply the function to the list of the different Animal by Year > combos this is what I get: > animals animalYears animalMass > bird 1 bird 1 23 > bird 2 bird 2 50 > cat 1 cat 1 15 > cat 2 <NA> <NA> <NA> > dog 1 dog 1 23 > dog 2 dog 2 38 > > What I expect is this: > > animals animalYears animalMass > 1 bird 1 41 > 2 bird 1 23 > 3 bird 2 23 > 4 bird 2 50 > 5 dog 1 49 > 6 dog 1 23 > 7 dog 2 13 > 8 dog 2 38 > 9 cat 1 42 > 10 cat 1 15 > 11 cat 2 33 > 12 NA NA NA > > Am I conditioning improperly within the function or am I missing something > else. Any help would be greatly appreciated. > > Best >
Curtis Burkhalter
2015-Mar-06 21:00 UTC
[R] problem with function that adds rows to dataframe based on conditional statement
Hey Tom, This solution works great, but if I try to then insert it in my function that I've displayed above and apply it to my split dataframe I get the error message: Error in `[.default`(xj, i) : invalid subscript type 'list' The reason why I need to try and get this to work within the function is that I'm trying to apply it to a much larger dataframe (nrow=12000,ncol=14). The actual data I'm working with consists of a sampling year, a site, and a bunch of response variables. For each sampling year by site combination I have 3 within year sampling occasions. For the sampling year by site combinations that don't have 3 sampling events I need to fill in the missing occasions with the NAs. Can you see why I might be getting this error message? Thanks On Fri, Mar 6, 2015 at 1:48 PM, Tom Wright <tom at maladmin.com> wrote:> If all you want is to add a row of na's could you just do something > like: > > nExpectedRows<-length(unique(animals)) * length(unique(animalYears)) * 2 > > newDf<-data.frame(animals=rep(NA,nExpectedRows-nrow(comAn)), > animalYears=rep(NA,nExpectedRows-nrow(comAn)), > animalMass=rep(NA,nExpectedRows-nrow(comAn))) > > comAn = rbind(comAn,newDf) > > > > On Thu, 2015-03-05 at 13:41 -0700, Curtis Burkhalter wrote: > > Hello everyone, > > > > I'm having a problem with a function that I wrote that is supposed to > add a > > row to dataframe based upon a conditional statement. To explain I've used > > an example below: > > > > #create data frame > > animals=c("bird","dog","cat") > > animals=rep(animals,each=4) > > animals=animals[1:11] > > animalYears=c(1,1,2,2,1,1,2,2,1,1,2) > > animalMass=round(runif(11,min=10,max=50),0) > > > > comAn=as.data.frame(cbind(animals,animalYears,animalMass)) > > comAn > > > > * animals* *animalYears* *animalMass* > > 1 bird 1 30 > > 2 bird 1 32 > > 3 bird 2 27 > > 4 bird 2 16 > > 5 dog 1 22 > > 6 dog 1 25 > > 7 dog 2 41 > > 8 dog 2 22 > > 9 cat 1 30 > > 10 cat 1 37 > > 11 cat 2 49 > > > > We can see here that for every type of animal I have two years of mass > > measurements, except for the cat in year 2. What I want to do is add an > > additional row to the end of the dataframe that consists strictly of NAs > > and then I can substitute those out later. > > > > So what I first did was split the 'comAn' dataframe into the different > > Animal by Year combos. > > > > #This line splits 'com_An' into a list ordered by the Animal by Year > combos > > comAn_split=split(comAn, paste(comAn$animals,comAn$animalYear)) > > > > Then I wrote the function that identifies whether a particular Animal by > > Year combo is less than two rows in length and if so it should add > another > > row that consists only of NAs using the vector 'NAs': > > > > #This function identifies the length of each Animal by Year combo and > then > > #uses the rbind function built in R to add a row > > #to each animal by year combo if they have less than 2 samples > > > > addNA <- function(comAn) { > > NAs=c(NA,NA,NA) > > ind <- seq_len(nrow(comAn)) > > comAn[ifelse(length(ind)<2,rbind(NAs),length(ind)),] > > } > > > > #This applies the function addNs to the animals data organized in list > > format > > addedNAcomAn <- do.call(rbind, lapply(comAn_split, addNA)) > > addedNAcomAn > > > > When I apply the function to the list of the different Animal by Year > > combos this is what I get: > > animals animalYears animalMass > > bird 1 bird 1 23 > > bird 2 bird 2 50 > > cat 1 cat 1 15 > > cat 2 <NA> <NA> <NA> > > dog 1 dog 1 23 > > dog 2 dog 2 38 > > > > What I expect is this: > > > > animals animalYears animalMass > > 1 bird 1 41 > > 2 bird 1 23 > > 3 bird 2 23 > > 4 bird 2 50 > > 5 dog 1 49 > > 6 dog 1 23 > > 7 dog 2 13 > > 8 dog 2 38 > > 9 cat 1 42 > > 10 cat 1 15 > > 11 cat 2 33 > > 12 NA NA NA > > > > Am I conditioning improperly within the function or am I missing > something > > else. Any help would be greatly appreciated. > > > > Best > > > > >-- Curtis Burkhalter https://sites.google.com/site/curtisburkhalter/ [[alternative HTML version deleted]]