Hi R -Users
I am sorry for bothering you. I was wondering what script can work to calculate
an immigration and extinction from two tables (time 1 and time 2). I could
easily calculate them in the Excel for small data set, but I have very big data
set. so that I was wondering to use R for this calculation. But I could not
figure it out to write these logical function in R. Would you provide me some
hints?
For example, I have these two tables
speciesTime1<-structure(list(site = 1:4, sp1 = c(0L, 1L, 1L, 0L), sp2 = c(0L,
1L, 1L, 0L), sp3 = c(1L, 0L, 1L, 0L)), .Names = c("site",
"sp1",
"sp2", "sp3"), class = "data.frame", row.names =
c(NA, -4L))
speciesTime2<-structure(list(site = 1:4, sp1 = c(1L, 0L, 1L, 1L), sp2 = c(0L,
1L, 1L, 1L), sp3 = c(1L, 1L, 0L, 1L)), .Names = c("site",
"sp1",
"sp2", "sp3"), class = "data.frame", row.names =
c(NA, -4L))
>From these two tables: I wanted to make the following two tables (Imm and
Exti]
[Imm means number of "sp" present in speciesTime2, but not in
speciesTime1]
imm<-structure(list(sp1 = c(1L, 0L, 0L, 1L), sp2 = c(0L, 0L, 0L, 1L
), sp3 = c(0L, 1L, 0L, 1L), sum = c(1L, 1L, 0L, 3L)), .Names =
c("sp1",
"sp2", "sp3", "sum"), class =
"data.frame", row.names = c(NA,
-4L))
[Exti=number of "sp" absent in speciesTime2, but present in
speciesTime1]
Exti<-structure(list(sp1 = c(0L, 1L, 0L, 0L), sp2 = c(0L, 0L, 0L, 0L
), sp3 = c(0L, 0L, 1L, 0L), sum = c(0L, 1L, 1L, 0L)), .Names =
c("sp1",
"sp2", "sp3", "sum"), class =
"data.frame", row.names = c(NA,
-4L))
Thanks
KG
==
[[alternative HTML version deleted]]
Hello,
Try the following. Maybe there are simpler ways, but it seems to work.
fun <- function(df1, df2){
pres.abs <- function(x, y){
x <- as.logical(x)
y <- as.logical(y)
1*(x & !y)
}
res <- matrix(nrow = nrow(df1), ncol = ncol(df1) - 1)
rownames(res) <- df1$site
colnames(res) <- colnames(df1)[-1]
for(st in df1$site){
for(sp in colnames(res)){
res[st, sp] <- pres.abs(df1[st, sp], df2[st, sp])
}
}
res <- cbind(res, sum = rowSums(res))
as.data.frame(res)
}
imm2 <- fun(speciesTime2, speciesTime1)
Exti2 <- fun(speciesTime1, speciesTime2)
Hope this helps,
Rui Barradas
Em 30-05-2013 22:28, Kristi Glover escreveu:> Hi R -Users
> I am sorry for bothering you. I was wondering what script can work to
calculate an immigration and extinction from two tables (time 1 and time 2). I
could easily calculate them in the Excel for small data set, but I have very
big data set. so that I was wondering to use R for this calculation. But I could
not figure it out to write these logical function in R. Would you provide me
some hints?
>
> For example, I have these two tables
> speciesTime1<-structure(list(site = 1:4, sp1 = c(0L, 1L, 1L, 0L), sp2 =
c(0L,
> 1L, 1L, 0L), sp3 = c(1L, 0L, 1L, 0L)), .Names = c("site",
"sp1",
> "sp2", "sp3"), class = "data.frame",
row.names = c(NA, -4L))
>
> speciesTime2<-structure(list(site = 1:4, sp1 = c(1L, 0L, 1L, 1L), sp2 =
c(0L,
> 1L, 1L, 1L), sp3 = c(1L, 1L, 0L, 1L)), .Names = c("site",
"sp1",
> "sp2", "sp3"), class = "data.frame",
row.names = c(NA, -4L))
>
>>From these two tables: I wanted to make the following two tables (Imm
and Exti]
> [Imm means number of "sp" present in speciesTime2, but not in
speciesTime1]
> imm<-structure(list(sp1 = c(1L, 0L, 0L, 1L), sp2 = c(0L, 0L, 0L, 1L
> ), sp3 = c(0L, 1L, 0L, 1L), sum = c(1L, 1L, 0L, 3L)), .Names =
c("sp1",
> "sp2", "sp3", "sum"), class =
"data.frame", row.names = c(NA,
> -4L))
>
> [Exti=number of "sp" absent in speciesTime2, but present in
speciesTime1]
>
> Exti<-structure(list(sp1 = c(0L, 1L, 0L, 0L), sp2 = c(0L, 0L, 0L, 0L
> ), sp3 = c(0L, 0L, 1L, 0L), sum = c(0L, 1L, 1L, 0L)), .Names =
c("sp1",
> "sp2", "sp3", "sum"), class =
"data.frame", row.names = c(NA,
> -4L))
> Thanks
> KG
> ==>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
I didn't follow your question in detail, but it sounds like the place to start is ?match and friends: ?intersect, ?setdiff, ?"%in%" ... Cheers, Bert On Thu, May 30, 2013 at 2:28 PM, Kristi Glover <kristi.glover at hotmail.com> wrote:> Hi R -Users > I am sorry for bothering you. I was wondering what script can work to calculate an immigration and extinction from two tables (time 1 and time 2). I could easily calculate them in the Excel for small data set, but I have very big data set. so that I was wondering to use R for this calculation. But I could not figure it out to write these logical function in R. Would you provide me some hints? > > For example, I have these two tables > speciesTime1<-structure(list(site = 1:4, sp1 = c(0L, 1L, 1L, 0L), sp2 = c(0L, > 1L, 1L, 0L), sp3 = c(1L, 0L, 1L, 0L)), .Names = c("site", "sp1", > "sp2", "sp3"), class = "data.frame", row.names = c(NA, -4L)) > > speciesTime2<-structure(list(site = 1:4, sp1 = c(1L, 0L, 1L, 1L), sp2 = c(0L, > 1L, 1L, 1L), sp3 = c(1L, 1L, 0L, 1L)), .Names = c("site", "sp1", > "sp2", "sp3"), class = "data.frame", row.names = c(NA, -4L)) > > >From these two tables: I wanted to make the following two tables (Imm and Exti] > [Imm means number of "sp" present in speciesTime2, but not in speciesTime1] > imm<-structure(list(sp1 = c(1L, 0L, 0L, 1L), sp2 = c(0L, 0L, 0L, 1L > ), sp3 = c(0L, 1L, 0L, 1L), sum = c(1L, 1L, 0L, 3L)), .Names = c("sp1", > "sp2", "sp3", "sum"), class = "data.frame", row.names = c(NA, > -4L)) > > [Exti=number of "sp" absent in speciesTime2, but present in speciesTime1] > > Exti<-structure(list(sp1 = c(0L, 1L, 0L, 0L), sp2 = c(0L, 0L, 0L, 0L > ), sp3 = c(0L, 0L, 1L, 0L), sum = c(0L, 1L, 1L, 0L)), .Names = c("sp1", > "sp2", "sp3", "sum"), class = "data.frame", row.names = c(NA, > -4L)) > Thanks > KG > ==> > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Not sure whether your data is just 0's and 1's, but something like this may also work for you: imm <- 1*(speciesTime2[ , 2:4] == 1 & speciesTime2[ , 2:4] != speciesTime1[ , 2:4]) imm <- cbind(imm, sum = rowSums(imm)) Exti <- 1*(speciesTime2[ , 2:4] == 0 & speciesTime2[ , 2:4] !speciesTime1[ , 2:4]) Exti <- cbind(Exti, sum = rowSums(Exti)) Peter On Thu, May 30, 2013 at 3:28 PM, Kristi Glover <kristi.glover@hotmail.com>wrote:> Hi R -Users > I am sorry for bothering you. I was wondering what script can work to > calculate an immigration and extinction from two tables (time 1 and time > 2). I could easily calculate them in the Excel for small data set, but I > have very big data set. so that I was wondering to use R for this > calculation. But I could not figure it out to write these logical function > in R. Would you provide me some hints? > > For example, I have these two tables > speciesTime1<-structure(list(site = 1:4, sp1 = c(0L, 1L, 1L, 0L), sp2 > c(0L, > 1L, 1L, 0L), sp3 = c(1L, 0L, 1L, 0L)), .Names = c("site", "sp1", > "sp2", "sp3"), class = "data.frame", row.names = c(NA, -4L)) > > speciesTime2<-structure(list(site = 1:4, sp1 = c(1L, 0L, 1L, 1L), sp2 > c(0L, > 1L, 1L, 1L), sp3 = c(1L, 1L, 0L, 1L)), .Names = c("site", "sp1", > "sp2", "sp3"), class = "data.frame", row.names = c(NA, -4L)) > > >From these two tables: I wanted to make the following two tables (Imm and > Exti] > [Imm means number of "sp" present in speciesTime2, but not in speciesTime1] > imm<-structure(list(sp1 = c(1L, 0L, 0L, 1L), sp2 = c(0L, 0L, 0L, 1L > ), sp3 = c(0L, 1L, 0L, 1L), sum = c(1L, 1L, 0L, 3L)), .Names = c("sp1", > "sp2", "sp3", "sum"), class = "data.frame", row.names = c(NA, > -4L)) > > [Exti=number of "sp" absent in speciesTime2, but present in speciesTime1] > > Exti<-structure(list(sp1 = c(0L, 1L, 0L, 0L), sp2 = c(0L, 0L, 0L, 0L > ), sp3 = c(0L, 0L, 1L, 0L), sum = c(0L, 1L, 1L, 0L)), .Names = c("sp1", > "sp2", "sp3", "sum"), class = "data.frame", row.names = c(NA, > -4L)) > Thanks > KG > ==> > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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. >[[alternative HTML version deleted]]
Or here's a simpler solution than you've been presented: # this is easier without the site column sp1 <- speciesTime1[,-1] sp2 <- speciesTime2[,-1] # will work for abundance as well as presence, as long as # the minimum abundance is not less than 1 imm <- matrix(0, nrow=nrow(sp1), ncol=ncol(sp1)) imm[sp1 < 1 & sp2 > 0] <- sp2[sp1 < 1 & sp2 > 0] colnames(imm) <- colnames(sp1) imm <- data.frame(imm, sum=rowSums(imm)) # Exti is calculated the same way. Sarah On Thu, May 30, 2013 at 5:28 PM, Kristi Glover <kristi.glover at hotmail.com> wrote:> Hi R -Users > I am sorry for bothering you. I was wondering what script can work to calculate an immigration and extinction from two tables (time 1 and time 2). I could easily calculate them in the Excel for small data set, but I have very big data set. so that I was wondering to use R for this calculation. But I could not figure it out to write these logical function in R. Would you provide me some hints? > > For example, I have these two tables > speciesTime1<-structure(list(site = 1:4, sp1 = c(0L, 1L, 1L, 0L), sp2 = c(0L, > 1L, 1L, 0L), sp3 = c(1L, 0L, 1L, 0L)), .Names = c("site", "sp1", > "sp2", "sp3"), class = "data.frame", row.names = c(NA, -4L)) > > speciesTime2<-structure(list(site = 1:4, sp1 = c(1L, 0L, 1L, 1L), sp2 = c(0L, > 1L, 1L, 1L), sp3 = c(1L, 1L, 0L, 1L)), .Names = c("site", "sp1", > "sp2", "sp3"), class = "data.frame", row.names = c(NA, -4L)) > > >From these two tables: I wanted to make the following two tables (Imm and Exti] > [Imm means number of "sp" present in speciesTime2, but not in speciesTime1] > imm<-structure(list(sp1 = c(1L, 0L, 0L, 1L), sp2 = c(0L, 0L, 0L, 1L > ), sp3 = c(0L, 1L, 0L, 1L), sum = c(1L, 1L, 0L, 3L)), .Names = c("sp1", > "sp2", "sp3", "sum"), class = "data.frame", row.names = c(NA, > -4L)) > > [Exti=number of "sp" absent in speciesTime2, but present in speciesTime1] > > Exti<-structure(list(sp1 = c(0L, 1L, 0L, 0L), sp2 = c(0L, 0L, 0L, 0L > ), sp3 = c(0L, 0L, 1L, 0L), sum = c(0L, 1L, 1L, 0L)), .Names = c("sp1", > "sp2", "sp3", "sum"), class = "data.frame", row.names = c(NA, > -4L)) > Thanks > KG > ==>-- http://www.functionaldiversity.org
Hi,
May be this helps:
speciesTime1New<- speciesTime1[,-1]
speciesTime2New<- speciesTime2[,-1]
imm1<-as.data.frame(matrix(0,4,3))
?imm1[]<-lapply(seq_len(ncol(speciesTime2New)),function(i)
ifelse(speciesTime2New[,i]==1&
speciesTime2New[,i]!=speciesTime1New[,i],1,0))
Ext1<-as.data.frame(matrix(0,4,3))
?Ext1[]<-lapply(seq_len(ncol(speciesTime1New)),function(i)
ifelse(speciesTime1New[,i]==1&
speciesTime1New[,i]!=speciesTime2New[,i],1,0))
library(plyr)
imm1New<-mutate(imm1,sum=rowSums(cbind(sp1,sp2,sp3)))
?Ext1New<-mutate(Ext1,sum=rowSums(cbind(sp1,sp2,sp3)))
imm1New
#? sp1 sp2 sp3 sum
#1?? 1?? 0?? 0?? 1
#2?? 0?? 0?? 1?? 1
#3?? 0?? 0?? 0?? 0
#4?? 1?? 1?? 1?? 3
Ext1New
#? sp1 sp2 sp3 sum
#1?? 0?? 0?? 0?? 0
#2?? 1?? 0?? 0?? 1
#3?? 0?? 0?? 1?? 1
#4?? 0?? 0?? 0?? 0
#or
imm2<-sapply(seq_len(ncol(speciesTime2New)),function(i)
ifelse(speciesTime2New[,i]==1&
speciesTime2New[,i]!=speciesTime1New[,i],1,0))
Ext2<-sapply(seq_len(ncol(speciesTime1New)),function(i)
ifelse(speciesTime1New[,i]==1&
speciesTime1New[,i]!=speciesTime2New[,i],1,0))
imm2$sum<-rowSums(imm2)
?Ext2<-as.data.frame(Ext2)
?Ext2$sum<-rowSums(Ext2)
colnames(imm2)<-colnames(imm1New)
colnames(Ext2)<-colnames(Ext1New)
?identical(imm2,imm1New)
#[1] TRUE
?identical(Ext2,Ext1New)
#[1] TRUE
A.K.
Hi R -Users
I am sorry for bothering you. I was wondering what script can work
to calculate an immigration and extinction from two tables (time 1 and
time 2). I could easily ?calculate them in the Excel for small data set,
but I have very big data set. so that I was wondering to use R for this
calculation. But I could not figure it out to write these logical
function in R. ?Would you provide me some hints?
?
For example, I have these two tables
speciesTime1<-structure(list(site = 1:4, sp1 = c(0L, 1L, 1L, 0L), sp2 = c(0L,
1L, 1L, 0L), sp3 = c(1L, 0L, 1L, 0L)), .Names = c("site",
"sp1",
"sp2", "sp3"), class = "data.frame", row.names =
c(NA, -4L))
speciesTime2<-structure(list(site = 1:4, sp1 = c(1L, 0L, 1L, 1L), sp2 = c(0L,
1L, 1L, 1L), sp3 = c(1L, 1L, 0L, 1L)), .Names = c("site",
"sp1",
"sp2", "sp3"), class = "data.frame", row.names =
c(NA, -4L))
>From these two tables: I wanted to make the following two tables (Imm and
Exti]
[Imm means number of "sp" present in speciesTime2, but not in
speciesTime1]
imm<-structure(list(sp1 = c(1L, 0L, 0L, 1L), sp2 = c(0L, 0L, 0L, 1L
), sp3 = c(0L, 1L, 0L, 1L), sum = c(1L, 1L, 0L, 3L)), .Names =
c("sp1",
"sp2", "sp3", "sum"), class =
"data.frame", row.names = c(NA,
-4L))
[Exti=number of "sp" absent in speciesTime2, but present in
speciesTime1]
Exti<-structure(list(sp1 = c(0L, 1L, 0L, 0L), sp2 = c(0L, 0L, 0L, 0L
), sp3 = c(0L, 0L, 1L, 0L), sum = c(0L, 1L, 1L, 0L)), .Names =
c("sp1",
"sp2", "sp3", "sum"), class =
"data.frame", row.names = c(NA,
-4L))
Thanks
KG
===