[R] How to convert category (or range/group) into continuous ?

2022-01-16 Thread Marna Wagley
Hi R users,
I first categorized the continuous data into groups (ranges or category)
but now I would like to change the category into a continuous data and
plot it. For example I have attached the data in which you can see two
columns named "group" and "value". The group column contains a range
(group, for example ), I am wondering how I can change the category data
(for example: "0,0.01]" )into continuous data with a 0.1 interval.
Thank you for your suggestions.

daT<-structure(list(group = c("(0,0.01]", "(0.01,0.025]", "(0.025,0.05]",
"(0.05,0.075]", "(0.075,0.1]", "(0.1,0.2]", "(0.2,0.3]", "(0.3,0.4]",
"(0.4,0.5]", "(0.5,0.6]", "(0.6,0.7]", "(0.7,0.8]", "(0.8,0.9]",
"(0.9,1]", "(1,1.1]", "(1.1,1.5]", "(1.5,2]", "(2,2.5]"), VALUE = c(1,
1, 1, 1, 1, 1, 0.9, 0.91667, 0.9, 0.85778,
0.82667, 0.66, 0.439090909, 0.328636364, 0.273409091, 0.245795455,
 0.218181818, 0.084848485)), class = "data.frame", row.names = c(NA,
-18L))
thanks,
MW

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] able to estimate in the excel but not in R, any suggestion?

2021-12-22 Thread Marna Wagley
Dear Jim,
Thank you very much for the help. The code seems to be right. Using this
code, I got exactly the same value as the excel's value.
This is great.
Thanks
MW

On Wed, Dec 22, 2021 at 10:57 PM jim holtman  wrote:

> You need to use the 'ifelse' function.  I think I copied down your
> formula and here is the output:
>
> > daT<-structure(list(sd = c(0.481, 0.682, 0.741, 0.394, 0.2, 0.655,
> 0.375),
> + mcd = c(51.305, 51.284, 51.249, 51.2, 51.137, 51.059, 50.968), ca =
> + c(49.313, 69.985, 75.914, 40.303, 20.493, 66.905,38.185)), class =
> + "data.frame", row.names = c(NA, -7L))
> > head(daT)
>  sdmcd ca
> 1 0.481 51.305 49.313
> 2 0.682 51.284 69.985
> 3 0.741 51.249 75.914
> 4 0.394 51.200 40.303
> 5 0.200 51.137 20.493
> 6 0.655 51.059 66.905
> >
> > # add in a new column with the calculation
> >
> > daT$ca_1 <- with(daT,
> +ifelse(sd > mcd * 2,
> +   pi * mcd ^ 2,
> +   (0.5 * sd) * sqrt(mcd^2 - (0.5 * sd)^2) +
> +   mcd^2 * asin((0.5 * sd) / (mcd)) * 2
> +   )
> + )
> >
> > daT
>  sdmcd ca ca_1
> 1 0.481 51.305 49.313 37.01651
> 2 0.682 51.284 69.985 52.46340
> 3 0.741 51.249 75.914 56.96310
> 4 0.394 51.200 40.303 30.25918
> 5 0.200 51.137 20.493 15.34110
> 6 0.655 51.059 66.905 50.16535
> 7 0.375 50.968 38.185 28.66948
> >
>
>
> Thanks
>
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
>
> On Wed, Dec 22, 2021 at 10:23 PM Marna Wagley 
> wrote:
> >
> > Hi R users,
> > I was trying to estimate some values in r but could not figure out how to
> > write the script in r. Although I was able to estimate it correctly in
> the
> > excel. For example I have the following data set.
> >
> > daT<-structure(list(sd = c(0.481, 0.682, 0.741, 0.394, 0.2, 0.655,
> 0.375),
> > mcd = c(51.305, 51.284, 51.249, 51.2, 51.137, 51.059, 50.968), ca =
> > c(49.313, 69.985, 75.914, 40.303, 20.493, 66.905,38.185)), class =
> > "data.frame", row.names = c(NA, -7L))
> > head(daT)
> >
> > In this data set, I need to estimate in the column name "ca", In the
> excel
> > I estimated the value using the following formula:
> >
> IF(A2>B2*2,PI()*B2^2,((0.5*A2)*SQRT(B2^2-(0.5*A2)^2)+B2^2*ASIN((0.5*A2)/B2))*2)
> >
> > But when I wrote the following code in the R, it did not work
> > attach(daT)
> >
> daT$ca<-if(sd>mcd*2,pi()*mcd^2,((0.5*sd)*sqrt(mcd^2-(0.5*sd)^2)+mcd^2*asin((0.5*sd)/mcd))*2)
> >
> > Your suggestion would be highly appreciated.
> >
> > Sincerely,
> >
> > MW
> >
> > [[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-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] able to estimate in the excel but not in R, any suggestion?

2021-12-22 Thread Marna Wagley
Hi R users,
I was trying to estimate some values in r but could not figure out how to
write the script in r. Although I was able to estimate it correctly in the
excel. For example I have the following data set.

daT<-structure(list(sd = c(0.481, 0.682, 0.741, 0.394, 0.2, 0.655, 0.375),
mcd = c(51.305, 51.284, 51.249, 51.2, 51.137, 51.059, 50.968), ca =
c(49.313, 69.985, 75.914, 40.303, 20.493, 66.905,38.185)), class =
"data.frame", row.names = c(NA, -7L))
head(daT)

In this data set, I need to estimate in the column name "ca", In the excel
I estimated the value using the following formula:
IF(A2>B2*2,PI()*B2^2,((0.5*A2)*SQRT(B2^2-(0.5*A2)^2)+B2^2*ASIN((0.5*A2)/B2))*2)

But when I wrote the following code in the R, it did not work
attach(daT)
daT$ca<-if(sd>mcd*2,pi()*mcd^2,((0.5*sd)*sqrt(mcd^2-(0.5*sd)^2)+mcd^2*asin((0.5*sd)/mcd))*2)

Your suggestion would be highly appreciated.

Sincerely,

MW

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] extracting bootstrap statistics by group with loop

2021-11-01 Thread Marna Wagley
Thank you Rui. It helped a lot.
-MW

On Sun, Oct 31, 2021 at 10:16 AM Rui Barradas  wrote:

> Hello,
>
> Now I'm spamming the list, not one of my days.
>
> My first post was right, there was no bug and the 2nd one was exactly
> the same code, it corrected nothing at all.
>
> Apologies for the noise,
>
> Rui Barradas
>
> Às 16:55 de 31/10/21, Rui Barradas escreveu:
> > Hello,
> >
> > Sorry, bug. In both by instructions it's boot_mean_se, not bootprop.
> >
> >
> > boot_year <- by(DaT, DaT$Year, boot_mean_se, statistic = bootprop, R = R)
> > boot_year_area <- by(DaT,
> >   INDICES = list(Year = DaT$Year, Area = DaT$Area),
> >   FUN = boot_mean_se,
> >   statistic = bootprop, R = R)
> >
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> > Às 16:48 de 31/10/21, Rui Barradas escreveu:
> >> Hello,
> >>
> >> Try to aggregate with ?by.
> >>
> >>
> >> bootprop <- function(data, index){
> >>d <- data[index, ]
> >>sum(d[["bothTimes"]], na.rm = TRUE)/sum(d[["total"]], na.rm = TRUE)#
> >> }
> >> boot_mean_se <- function(data, statistic, R){
> >>b <- boot::boot(DaT, bootprop, R = R)
> >>c(bootMean = mean(b$t), bootSE = sd(b$t))
> >> }
> >>
> >> boot_year <- by(DaT, DaT$Year, boot_mean_se, statistic = bootprop, R =
> R)
> >> boot_year_area <- by(DaT,
> >>   INDICES = list(Year = DaT$Year, Area = DaT$Area),
> >>   FUN = boot_mean_se,
> >>   statistic = bootprop, R = R)
> >> boot_year
> >> boot_year_area
> >>
> >> boot_year <- do.call(rbind, boot_year)
> >>
> >> d <- dimnames(boot_year_area)
> >> boot_year_area <- cbind(Reduce(expand.grid, rev(d))[2:1],
> >>  do.call(rbind, boot_year_area))
> >> names(boot_year_area)[1:2] <- names(d)
> >> boot_year_area
> >>
> >>
> >> Hope this helps,
> >>
> >> Rui Barradas
> >>
> >> Às 11:47 de 31/10/21, Marna Wagley escreveu:
> >>> Hi R users,
> >>> I was trying to extract the bootstrap mean and its SE by group but I
> >>> have
> >>> been doing  it by separating the group manually. The data set is big so
> >>> doing it manually is a kind of tedious task. I am wondering whether
> >>> there
> >>> is a possibility to do it by creating a loop. I am weak in writing loop
> >>> functions. I am attaching an example data and how I performed the
> >>> analysis, see below.
> >>> Thanks for your help.
> >>> Sincerely,
> >>> MW
> >>> 
> >>> library(boot)
> >>> DaT<-structure(list(bothTimes = c(0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L,
> >>> 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L),
> >>> total = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> >>> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Area = c("A",
> >>> "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "A", "A",
> >>> "A", "A", "A", "A", "B", "B", "B", "B", "B", "B"), Year = c(2015L,
> >>> 2015L,
> >>> 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
> >>> 2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> >>> 2016L, 2016L, 2016L, 2016L, 2016L)), class = "data.frame", row.names =
> >>> c(NA, -24L))
> >>>
> >>> head(DaT)
> >>> R=100
> >>> bootprop <- function(data, index){
> >>>d <- data[index, ]
> >>>sum(d[["bothTimes"]], na.rm = TRUE)/sum(d[["total"]], na.rm = TRUE)#
> >>> }
> >>>
> >>> ###
> >>> #2015
> >>> ###
> >>> #-Year2015_pooled
> >>> Y2015_pooled<-subset(DaT, DaT$Year=="2015")
> >>> Y2015_pooled_boot <- boot(Y2015_pooled, bootprop, R)
> >>> boot_Y2015_pooled<-data.frame(Year="2015", Area= "Pooled", bootMean=
> >>> Y2015_pooled_boot$t0, SE=sd(Y2015_pooled_boot$t))
> >

[R] extracting bootstrap statistics by group with loop

2021-10-31 Thread Marna Wagley
Hi R users,
I was trying to extract the bootstrap mean and its SE by group but I have
been doing  it by separating the group manually. The data set is big so
doing it manually is a kind of tedious task. I am wondering whether there
is a possibility to do it by creating a loop. I am weak in writing loop
functions. I am attaching an example data and how I performed the
analysis, see below.
Thanks for your help.
Sincerely,
MW

library(boot)
DaT<-structure(list(bothTimes = c(0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L),
total = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Area = c("A",
"A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "A", "A",
"A", "A", "A", "A", "B", "B", "B", "B", "B", "B"), Year = c(2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L)), class = "data.frame", row.names =
c(NA, -24L))

head(DaT)
R=100
bootprop <- function(data, index){
  d <- data[index, ]
  sum(d[["bothTimes"]], na.rm = TRUE)/sum(d[["total"]], na.rm = TRUE)#
}

###
#2015
###
#-Year2015_pooled
Y2015_pooled<-subset(DaT, DaT$Year=="2015")
Y2015_pooled_boot <- boot(Y2015_pooled, bootprop, R)
boot_Y2015_pooled<-data.frame(Year="2015", Area= "Pooled", bootMean=
Y2015_pooled_boot$t0, SE=sd(Y2015_pooled_boot$t))
#-Year2015_AreaA
Y2015_A<-subset(DaT, DaT$Year=="2015" & DaT$Area=="A")
Y2015_A_boot <- boot(Y2015_A, bootprop, R)
boot_Y2015_A<-data.frame(Year="2015", Area= "A", bootMean= Y2015_A_boot$t0,
SE=sd(Y2015_A_boot$t))
#Year2015_AreaB
Y2015_B<-subset(DaT, DaT$Year=="2015" & DaT$Area=="B")
Y2015_B_boot <- boot(Y2015_B, bootprop, R)
boot_Y2015_B<-data.frame(Year="2015", Area= "B", bootMean= Y2015_B_boot$t0,
SE=sd(Y2015_B_boot$t))
###
#2016
###
#-Year2016_pooled
Y2016_pooled<-subset(DaT, DaT$Year=="2016")
Y2016_pooled_boot <- boot(Y2016_pooled, bootprop, R)
boot_Y2016_pooled<-data.frame(Year="2016", Area= "Pooled", bootMean=
Y2016_pooled_boot$t0, SE=sd(Y2016_pooled_boot$t))

#-Year2016_AreaA
Y2016_A<-subset(DaT, DaT$Year=="2016" & DaT$Area=="A")
Y2016_A_boot <- boot(Y2016_A, bootprop, R)

boot_Y2016_A<-data.frame(Year="2016", Area= "A", bootMean= Y2016_A_boot$t0,
SE=sd(Y2016_A_boot$t))
#Year2016_AreaB
Y2016_B<-subset(DaT, DaT$Year=="2016" & DaT$Area=="B")
Y2016_B_boot <- boot(Y2016_B, bootprop, R)
boot_Y2016_B<-data.frame(Year="2016", Area= "B", bootMean= Y2016_B_boot$t0,
SE=sd(Y2016_B_boot$t))

## output data.matrix
BootMean_All<-rbind(boot_Y2015_pooled,boot_Y2015_A,boot_Y2015_B,boot_Y2016_pooled,boot_Y2016_A,boot_Y2016_B)
BootMean_All

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] assigning suitability index value

2021-08-11 Thread Marna Wagley
Thank you Jeff. I think the code you wrote works. The value I put in the
output was just guessing by looking at the graph.
Thank you once again Jeff.

temp<-structure(list(X = c(1468285.96, 1468476.96, 1468479.96, 1468482.96,
1468485.96, 1468467.96, 1468470.96, 1468473.96, 1468476.96, 1468479.96,
1468482.96, 1468485.96, 1468458.96, 1468461.96, 1468464.96, 1468467.96,
1468470.96, 1468473.96, 1468476.96), Y = c(415099.27, 415096.27, 415096.27,
415096.27, 415096.27, 415093.27, 415093.27, 415093.27, 415093.27, 415093.27,
415093.27, 415093.27, 415090.27, 415090.27, 415090.27, 415090.27, 415090.27,
415090.27, 415090.27), temp = c(1.959473, 15.092773, 15.128174, 14.368896,
9.892578, 15.720215, 15.767822, 15.26001, 14.642334, 14.6521, 13.916016,
10.3479, 16.052246, 16.094971, 15.167236, 15.455322, 15.472412, 24.741211,
14.755859)), class = "data.frame", row.names = c(NA, -19L))


print(temp)


table2<-structure(list(temp = c(0L, 10L, 15L, 17L, 25L, 30L), Index = c(0,
 0.3, 1, 1, 0.5, 0)), class = "data.frame", row.names = c(NA, -6L))

print(table2)


ggplot(data=table2, aes(x=temp, y=Index)) +geom_path()+geom_point()



Output<-structure(list(X = c(1468285.96, 1468476.96, 1468479.96, 1468482.96,
1468485.96, 1468467.96, 1468470.96, 1468473.96, 1468476.96, 1468479.96,
1468482.96, 1468485.96, 1468458.96, 1468461.96, 1468464.96, 1468467.96,
1468470.96, 1468473.96, 1468476.96), Y = c(415099.27, 415096.27, 415096.27,
415096.27, 415096.27, 415093.27, 415093.27, 415093.27, 415093.27, 415093.27,
415093.27, 415093.27, 415090.27, 415090.27, 415090.27, 415090.27, 415090.27,
415090.27, 415090.27), temp = c(1.959473, 0.092773, 15.128174, 14.368896,
9.892578, 15.720215, 15.767822, 15.26001, 14.642334, 14.6521, 13.916016,
10.3479, 16.052246, 16.094971, 15.167236, 15.455322, 15.472412, 24.741211,
14.755859), index = c(0.012, 0.001, 1, 0.9, 0.31, 1, 1, 1, 0.91, 0.921,
0.824, 0.254, 1, 1, 1, 1, 1, 0.652, 0.93)), class="data.frame", row.names =
c(NA, -19L))


print(Output)

On Tue, Aug 10, 2021 at 11:16 PM Jeff Newmiller 
wrote:

> Piecewise linear interpolation is implemented in the ?approx function. It
> does not agree exactly with your Output, I don't know if there is something
> else you are accounting for it if your Output is in error.
>
> temp$index <- approx( table2$temp, table2$Index, temp$temp )$y
>
> BTW your code was usable but messed up... please set your email program to
> send plain text email so your formatting does not mess with your code.
>
>
> On August 10, 2021 10:30:57 PM PDT, Marna Wagley 
> wrote:
> >Hi R Users,
> >I have two tables, one is temperature data (temp) and another table is a
> >suitability index. I wanted to assign the suitability index value in the
> >temperature data (temp) based on Table 2 (or graph, which is a suitability
> >curve), but I could not figure it out.
> >Are there any suggestions for me how I can assign the suitability index
> >value in table1 (temp) based on the suitability graph? I have a very big
> >data set but showing only a few data to illustrate the problem.
> >
> >temp<-structure(list(X = c(1468285.96, 1468476.96, 1468479.96, 1468482.96,
> >
> >1468485.96, 1468467.96, 1468470.96, 1468473.96, 1468476.96, 1468479.96,
> >
> >1468482.96, 1468485.96, 1468458.96, 1468461.96, 1468464.96, 1468467.96,
> >
> >1468470.96, 1468473.96, 1468476.96), Y = c(415099.27, 415096.27,
> >
> >415096.27, 415096.27, 415096.27, 415093.27, 415093.27, 415093.27,
> >
> >415093.27, 415093.27, 415093.27, 415093.27, 415090.27, 415090.27,
> >
> >415090.27, 415090.27, 415090.27, 415090.27, 415090.27), temp = c(1.959473,
> >
> >15.092773, 15.128174, 14.368896, 9.892578, 15.720215, 15.767822,
> >
> >15.26001, 14.642334, 14.6521, 13.916016, 10.3479, 16.052246,
> >
> >16.094971, 15.167236, 15.455322, 15.472412, 24.741211, 14.755859
> >
> >)), class = "data.frame", row.names = c(NA, -19L))
> >
> >
> >print(temp)
> >
> >
> >table2<-structure(list(temp = c(0L, 10L, 15L, 17L, 25L, 30L), Index = c(0,
> >
> >0.3, 1, 1, 0.5, 0)), class = "data.frame", row.names = c(NA,
> >
> >-6L))
> >
> >print(table2)
> >
> >
> >ggplot(data=table2, aes(x=temp, y=Index)) +
> >
> >  geom_path()+
> >
> >  geom_point()
> >
> >
> >
> ># now I would like to assign the index value of table 2 into table 1
> >(temp), and I was looking for the following table as an output. The index
> >value in the output I put manually.
> >
> >
> >Output<-structure(list(X = c(1468285.96, 1468476.96, 1468479.96,
> 1468482.96,
> > 1468485.96, 1468467.96, 1468470.96, 1468473.96, 1468476.96, 1468479.96,
&

[R] assigning suitability index value

2021-08-10 Thread Marna Wagley
Hi R Users,
I have two tables, one is temperature data (temp) and another table is a
suitability index. I wanted to assign the suitability index value in the
temperature data (temp) based on Table 2 (or graph, which is a suitability
curve), but I could not figure it out.
Are there any suggestions for me how I can assign the suitability index
value in table1 (temp) based on the suitability graph? I have a very big
data set but showing only a few data to illustrate the problem.

temp<-structure(list(X = c(1468285.96, 1468476.96, 1468479.96, 1468482.96,

1468485.96, 1468467.96, 1468470.96, 1468473.96, 1468476.96, 1468479.96,

1468482.96, 1468485.96, 1468458.96, 1468461.96, 1468464.96, 1468467.96,

1468470.96, 1468473.96, 1468476.96), Y = c(415099.27, 415096.27,

415096.27, 415096.27, 415096.27, 415093.27, 415093.27, 415093.27,

415093.27, 415093.27, 415093.27, 415093.27, 415090.27, 415090.27,

415090.27, 415090.27, 415090.27, 415090.27, 415090.27), temp = c(1.959473,

15.092773, 15.128174, 14.368896, 9.892578, 15.720215, 15.767822,

15.26001, 14.642334, 14.6521, 13.916016, 10.3479, 16.052246,

16.094971, 15.167236, 15.455322, 15.472412, 24.741211, 14.755859

)), class = "data.frame", row.names = c(NA, -19L))


print(temp)


table2<-structure(list(temp = c(0L, 10L, 15L, 17L, 25L, 30L), Index = c(0,

0.3, 1, 1, 0.5, 0)), class = "data.frame", row.names = c(NA,

-6L))

print(table2)


ggplot(data=table2, aes(x=temp, y=Index)) +

  geom_path()+

  geom_point()



# now I would like to assign the index value of table 2 into table 1
(temp), and I was looking for the following table as an output. The index
value in the output I put manually.


Output<-structure(list(X = c(1468285.96, 1468476.96, 1468479.96, 1468482.96,
 1468485.96, 1468467.96, 1468470.96, 1468473.96, 1468476.96, 1468479.96,

1468482.96, 1468485.96, 1468458.96, 1468461.96, 1468464.96, 1468467.96,

1468470.96, 1468473.96, 1468476.96), Y = c(415099.27, 415096.27,

415096.27, 415096.27, 415096.27, 415093.27, 415093.27, 415093.27,

415093.27, 415093.27, 415093.27, 415093.27, 415090.27, 415090.27,

415090.27, 415090.27, 415090.27, 415090.27, 415090.27), temp = c(1.959473,

0.092773, 15.128174, 14.368896, 9.892578, 15.720215, 15.767822,

15.26001, 14.642334, 14.6521, 13.916016, 10.3479, 16.052246,

16.094971, 15.167236, 15.455322, 15.472412, 24.741211, 14.755859

), index = c(0.012, 0.001, 1, 0.9, 0.31, 1, 1, 1, 0.91, 0.921,

0.824, 0.254, 1, 1, 1, 1, 1, 0.652, 0.93)), class = "data.frame", row.names
= c(NA,

-19L))


print(Output)


Thank you very much for your help.

MW

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Conditional extraction of values in a data.frame in r

2021-04-22 Thread Marna Wagley
Hi R Users,
I have been struggling to extract the data based on conditional values in
different columns. I have a very big dataset (rows) and a couple of
columns. here an example of the dataset is:

daT<-structure(list(ID = c("id1", "id2", "id3", "id4", "id5", "id6",

"id7"), First_detectiondate = c("7/21/2015", "5/19/2015", "5/27/2015",

NA, "9/25/2015", NA, NA), Second_detectiondate = c(NA, NA, "6/1/2015",

"5/29/2015", NA, NA, "4/17/2015"), third_detectiondate = c(NA,

"5/21/2015", "6/20/2015", NA, NA, "", NA)), class = "data.frame", row.names
= c(NA,

-7L))


head(daT)


I wanted to put conditions such as: if any of the columns of 2.3.4 has a
date, get the date which was latest. If there is no date, put NA. and I was
looking for the output as shown in the following table.


output<-structure(list(ID = c("id1", "id2", "id3", "id4", "id5", "id6",

"id7"), First_detectiondate = c("7/21/2015", "5/19/2015", "5/27/2015",

NA, "9/25/2015", NA, NA), Second_detectiondate = c(NA, NA, "6/1/2015",

"5/29/2015", NA, NA, "4/17/2015"), third_detectiondate = c(NA,

"5/21/2015", "6/20/2015", NA, NA, "", NA), output1 = c("7/21/2015",

"5/21/2015", "6/20/2015", "5/29/2015", "9/25/2015", NA, "4/17/2015"

)), class = "data.frame", row.names = c(NA, -7L))

head(output)



Is there a way to get the table similar to the table "output"?


Thank you very much for your help.


Sincerely,


MW

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] extraction of last observation date from many observations in each row/column

2021-04-02 Thread Marna Wagley
Dear R-Users,
I have a dataset containing more than two observation dates in some of the
columns and sites  but I wanted to extract only the last date of the
observation.
Is there any easiest way to get that last observation in each column/row?

Here is the example data:
daT<-structure(list(ID = c("M3", "M5", "M1"), Site1 =
c("12/20/2018,12/28/2018",
"12/17/18", ""), Site2 = c("", "", "1/19/2019"), Site3 = c("9/25/2019", "",
"1/10/2019, 1/11/2019, 1/17/2019")), class = "data.frame", row.names =
c(NA,-3L))

#I wanted to make the table like this:
output<-structure(list(ID = c("M3", "M5", "M1"), Site1 = c("12/28/2018",
"12/7/2018", ""), Site2 = c("", "", "1/19/2019"), Site3 = c("9/25/2019",
 "", "1/17/2019")), class = "data.frame", row.names = c(NA, -3L))

Thank you for your help.
Sincerely,
MW

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to generate SE for the proportion value using a randomization process in R?

2021-01-28 Thread Marna Wagley
Thank you Rui,
This is great. How about the following?

SimilatedData<-boot.array(b, indices=T)

seems it is giving the rows ID which are used in the calculation, isn't it?




On Thu, Jan 28, 2021 at 12:21 PM Rui Barradas  wrote:

> Hello,
>
> I don't know why you would need to see the indices but rewrite the
> function bootprop as
>
> bootprop_ind <- function(data, index){
>d <- data[index, ]
>#sum(d[["BothTimes"]], na.rm = TRUE)/sum(d[["Time1"]], na.rm = TRUE)
>index
> }
>
>
> and call in the same way. It will now return a matrix of indices with R
> = 1000 rows and 19 columns.
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 19:29 de 28/01/21, Marna Wagley escreveu:
> > Hi Rui,
> > I am sorry for asking you several questions.
> >
> > In the given example, randomizations (reshuffle) were done 1000 times,
> > and its 1000 proportion values (results) are stored and it can be seen
> > using b$t; but I was wondering how the table was randomized (which rows
> > have been missed/or repeated in each randomizing procedure?).
> >
> > Is there any way we can see the randomized table and its associated
> > results? Here in this example, I randomized (or bootstrapped) the table
> > into three times (R=3) so I would like to store these three tables and
> > look at them later to know which rows were repeated/missed. Is there any
> > possibility?
> > The example data and the code is given below.
> >
> > Thank you for your help.
> >
> > 
> > library(boot)
> > dat<-structure(list(Sample = structure(c(1L, 12L, 13L, 14L, 15L, 16L,
> > 17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label =
> c("id1",
> > "id10", "id11", "id12", "id13", "id14", "id15", "id16", "id17",
> > "id18", "id19", "Id2", "id3", "id4", "id5", "id6", "id7", "id8",
> > "id9"), class = "factor"), Time1 = c(0L, 1L, 1L, 1L, 0L, 0L,
> > 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L), Time2 = c(1L,
> > 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L,
> > 1L, 1L)), .Names = c("Sample", "Time1", "Time2"), class = "data.frame",
> > row.names = c(NA,
> > -19L))
> > daT<-data.frame(dat %>%
> >mutate(Time1.but.not.in.Time2 = case_when(
> >  Time1 %in% "1" & Time2 %in% "0"  ~ "1"),
> > Time2.but.not.in.Time1 = case_when(
> >  Time1 %in% "0" & Time2 %in% "1"  ~ "1"),
> >   BothTimes = case_when(
> >  Time1 %in% "1" & Time2 %in% "1"  ~ "1")))
> > cols.num <- c("Time1.but.not.in.Time2","Time2.but.not.in.Time1",
> > "BothTimes")
> > daT[cols.num] <- sapply(daT[cols.num],as.numeric)
> > summary(daT)
> >
> > bootprop <- function(data, index){
> > d <- data[index, ]
> > sum(d[["BothTimes"]], na.rm = TRUE)/sum(d[["Time1"]], na.rm = TRUE)
> > }
> >
> > R <- 3
> > set.seed(2020)
> > b <- boot(daT, bootprop, R)
> > b
> > b$t0 # original
> > b$t
> > sd(b$t)  # bootstrapped estimate of the SE of the sample prop.
> > hist(b$t, freq = FALSE)
> >
> > str(b)
> > b$data
> > b$seed
> > b$sim
> > b$strata
> > 
> >
> >
> > On Sat, Jan 23, 2021 at 12:36 AM Marna Wagley  > <mailto:marna.wag...@gmail.com>> wrote:
> >
> > Yes Rui, I can see we don't need to divide by square root of sample
> > size. The example is great to understand it.
> > Thank you.
> > Marna
> >
> >
> > On Sat, Jan 23, 2021 at 12:28 AM Rui Barradas  > <mailto:ruipbarra...@sapo.pt>> wrote:
> >
> > Hello,
> >
> > Inline.
> >
> > Às 07:47 de 23/01/21, Marna Wagley escreveu:
> >  > Dear Rui,
> >  > I was wondering whether we have to square root of SD to find
> > SE, right?
> >
> > No, we don't. var already divides by n, don't divide again.
> > This is the code, that can be seen by running the function name
> > at a
> > command line.
> >
> >
> > sd
> > #function (x, na.rm = FALSE)
> > #sqrt(var(if (is.vector(x) ||

Re: [R] How to generate SE for the proportion value using a randomization process in R?

2021-01-28 Thread Marna Wagley
Hi Rui,
I am sorry for asking you several questions.

In the given example, randomizations (reshuffle) were done 1000 times, and
its 1000 proportion values (results) are stored and it can be seen using
b$t; but I was wondering how the table was randomized (which rows have been
missed/or repeated in each randomizing procedure?).

Is there any way we can see the randomized table and its associated
results? Here in this example, I randomized (or bootstrapped) the table
into three times (R=3) so I would like to store these three tables and look
at them later to know which rows were repeated/missed. Is there any
possibility?
The example data and the code is given below.

Thank you for your help.


library(boot)
dat<-structure(list(Sample = structure(c(1L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label = c("id1",
"id10", "id11", "id12", "id13", "id14", "id15", "id16", "id17",
"id18", "id19", "Id2", "id3", "id4", "id5", "id6", "id7", "id8",
"id9"), class = "factor"), Time1 = c(0L, 1L, 1L, 1L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L), Time2 = c(1L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L,
1L, 1L)), .Names = c("Sample", "Time1", "Time2"), class = "data.frame",
row.names = c(NA,
-19L))
daT<-data.frame(dat %>%
  mutate(Time1.but.not.in.Time2 = case_when(
Time1 %in% "1" & Time2 %in% "0"  ~ "1"),
Time2.but.not.in.Time1 = case_when(
Time1 %in% "0" & Time2 %in% "1"  ~ "1"),
 BothTimes = case_when(
Time1 %in% "1" & Time2 %in% "1"  ~ "1")))
cols.num <- c("Time1.but.not.in.Time2","Time2.but.not.in.Time1",
"BothTimes")
daT[cols.num] <- sapply(daT[cols.num],as.numeric)
summary(daT)

bootprop <- function(data, index){
   d <- data[index, ]
   sum(d[["BothTimes"]], na.rm = TRUE)/sum(d[["Time1"]], na.rm = TRUE)
}

R <- 3
set.seed(2020)
b <- boot(daT, bootprop, R)
b
b$t0 # original
b$t
sd(b$t)  # bootstrapped estimate of the SE of the sample prop.
hist(b$t, freq = FALSE)

str(b)
b$data
b$seed
b$sim
b$strata



On Sat, Jan 23, 2021 at 12:36 AM Marna Wagley 
wrote:

> Yes Rui, I can see we don't need to divide by square root of sample size.
> The example is great to understand it.
> Thank you.
> Marna
>
>
> On Sat, Jan 23, 2021 at 12:28 AM Rui Barradas 
> wrote:
>
>> Hello,
>>
>> Inline.
>>
>> Às 07:47 de 23/01/21, Marna Wagley escreveu:
>> > Dear Rui,
>> > I was wondering whether we have to square root of SD to find SE, right?
>>
>> No, we don't. var already divides by n, don't divide again.
>> This is the code, that can be seen by running the function name at a
>> command line.
>>
>>
>> sd
>> #function (x, na.rm = FALSE)
>> #sqrt(var(if (is.vector(x) || is.factor(x)) x else as.double(x),
>> #na.rm = na.rm))
>> #
>> #
>>
>>
>>
>> >
>> > bootprop <- function(data, index){
>> > d <- data[index, ]
>> > sum(d[["BothTimes"]], na.rm = TRUE)/sum(d[["Time1"]], na.rm = TRUE)
>> > }
>> >
>> > R <- 1e3
>> > set.seed(2020)
>> > b <- boot(daT, bootprop, R)
>> > b
>> > b$t0 # original
>> > sd(b$t)  # bootstrapped estimate of the SE of the sample prop.
>> > sd(b$t)/sqrt(1000)
>> > pandit*(1-pandit)
>> >
>> > hist(b$t, freq = FALSE)
>>
>>
>> Try plotting the normal densities for both cases, the red line is
>> clearly wrong.
>>
>>
>> f <- function(x, xbar, s){
>>dnorm(x, mean = xbar, sd = s)
>> }
>>
>> hist(b$t, freq = FALSE)
>> curve(f(x, xbar = b$t0, s = sd(b$t)), from = 0, to = 1, col = "blue",
>> add = TRUE)
>> curve(f(x, xbar = b$t0, s = sd(b$t)/sqrt(R)), from = 0, to = 1, col =
>> "red", add = TRUE)
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> >
>> >
>> >
>> >
>> > On Fri, Jan 22, 2021 at 3:07 PM Rui Barradas > > <mailto:ruipbarra...@sapo.pt>> wrote:
>> >
>> > Hello,
>> >
>> > Something like this, using base package boot?
>> >
>> >
>> > library(boot)
>> >
>> > bootprop <- function(data, index){
>> > d <- 

Re: [R] How to generate SE for the proportion value using a randomization process in R?

2021-01-23 Thread Marna Wagley
Yes Rui, I can see we don't need to divide by square root of sample size.
The example is great to understand it.
Thank you.
Marna


On Sat, Jan 23, 2021 at 12:28 AM Rui Barradas  wrote:

> Hello,
>
> Inline.
>
> Às 07:47 de 23/01/21, Marna Wagley escreveu:
> > Dear Rui,
> > I was wondering whether we have to square root of SD to find SE, right?
>
> No, we don't. var already divides by n, don't divide again.
> This is the code, that can be seen by running the function name at a
> command line.
>
>
> sd
> #function (x, na.rm = FALSE)
> #sqrt(var(if (is.vector(x) || is.factor(x)) x else as.double(x),
> #na.rm = na.rm))
> #
> #
>
>
>
> >
> > bootprop <- function(data, index){
> > d <- data[index, ]
> > sum(d[["BothTimes"]], na.rm = TRUE)/sum(d[["Time1"]], na.rm = TRUE)
> > }
> >
> > R <- 1e3
> > set.seed(2020)
> > b <- boot(daT, bootprop, R)
> > b
> > b$t0 # original
> > sd(b$t)  # bootstrapped estimate of the SE of the sample prop.
> > sd(b$t)/sqrt(1000)
> > pandit*(1-pandit)
> >
> > hist(b$t, freq = FALSE)
>
>
> Try plotting the normal densities for both cases, the red line is
> clearly wrong.
>
>
> f <- function(x, xbar, s){
>dnorm(x, mean = xbar, sd = s)
> }
>
> hist(b$t, freq = FALSE)
> curve(f(x, xbar = b$t0, s = sd(b$t)), from = 0, to = 1, col = "blue",
> add = TRUE)
> curve(f(x, xbar = b$t0, s = sd(b$t)/sqrt(R)), from = 0, to = 1, col =
> "red", add = TRUE)
>
>
> Hope this helps,
>
> Rui Barradas
>
> >
> >
> >
> >
> > On Fri, Jan 22, 2021 at 3:07 PM Rui Barradas  > <mailto:ruipbarra...@sapo.pt>> wrote:
> >
> > Hello,
> >
> > Something like this, using base package boot?
> >
> >
> > library(boot)
> >
> > bootprop <- function(data, index){
> > d <- data[index, ]
> >     sum(d[["BothTimes"]], na.rm = TRUE)/sum(d[["Time1"]], na.rm =
> TRUE)
> > }
> >
> > R <- 1e3
> > set.seed(2020)
> > b <- boot(daT, bootprop, R)
> > b
> > b$t0 # original
> > sd(b$t)  # bootstrapped estimate of the SE of the sample prop.
> > hist(b$t, freq = FALSE)
> >
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> > Às 21:57 de 22/01/21, Marna Wagley escreveu:
> >  > Hi All,
> >  > I was trying to estimate standard error (SE) for the proportion
> > value using
> >  > some kind of randomization process (bootstrapping or jackknifing)
> > in R, but
> >  > I could not figure it out.
> >  >
> >  > Is there any way to generate SE for the proportion?
> >  >
> >  > The example of the data and the code I am using is attached for
> your
> >  > reference. I would like to generate the value of proportion with
> > a SE using
> >  > a 1000 times randomization.
> >  >
> >  > dat<-structure(list(Sample = structure(c(1L, 12L, 13L, 14L, 15L,
> 16L,
> >  > 17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label
> > = c("id1",
> >  > "id10", "id11", "id12", "id13", "id14", "id15", "id16", "id17",
> >  > "id18", "id19", "Id2", "id3", "id4", "id5", "id6", "id7", "id8",
> >  > "id9"), class = "factor"), Time1 = c(0L, 1L, 1L, 1L, 0L, 0L,
> >  > 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L), Time2 = c(1L,
> >  > 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L,
> >  > 1L, 1L)), .Names = c("Sample", "Time1", "Time2"), class =
> > "data.frame",
> >  > row.names = c(NA,
> >  > -19L))
> >  > daT<-data.frame(dat %>%
> >  >mutate(Time1.but.not.in.Time2 = case_when(
> >  >  Time1 %in% "1" & Time2 %in% "0"  ~ "1"),
> >  > Time2.but.not.in.Time1 = case_when(
> >  >  Time1 %in% "0" & Time2 %in% "1"  ~ "1"),
> >  >   BothTimes = case_when(
> >  >  Time1 %in% "1" & Time2 %in% "1"  ~ "1")))
> >  >   daT
> > 

Re: [R] How to generate SE for the proportion value using a randomization process in R?

2021-01-22 Thread Marna Wagley
Dear Rui,
I was wondering whether we have to square root of SD to find SE, right?

bootprop <- function(data, index){
   d <- data[index, ]
   sum(d[["BothTimes"]], na.rm = TRUE)/sum(d[["Time1"]], na.rm = TRUE)
}

R <- 1e3
set.seed(2020)
b <- boot(daT, bootprop, R)
b
b$t0 # original
sd(b$t)  # bootstrapped estimate of the SE of the sample prop.
sd(b$t)/sqrt(1000)
pandit*(1-pandit)

hist(b$t, freq = FALSE)




On Fri, Jan 22, 2021 at 3:07 PM Rui Barradas  wrote:

> Hello,
>
> Something like this, using base package boot?
>
>
> library(boot)
>
> bootprop <- function(data, index){
>d <- data[index, ]
>sum(d[["BothTimes"]], na.rm = TRUE)/sum(d[["Time1"]], na.rm = TRUE)
> }
>
> R <- 1e3
> set.seed(2020)
> b <- boot(daT, bootprop, R)
> b
> b$t0 # original
> sd(b$t)  # bootstrapped estimate of the SE of the sample prop.
> hist(b$t, freq = FALSE)
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 21:57 de 22/01/21, Marna Wagley escreveu:
> > Hi All,
> > I was trying to estimate standard error (SE) for the proportion value
> using
> > some kind of randomization process (bootstrapping or jackknifing) in R,
> but
> > I could not figure it out.
> >
> > Is there any way to generate SE for the proportion?
> >
> > The example of the data and the code I am using is attached for your
> > reference. I would like to generate the value of proportion with a SE
> using
> > a 1000 times randomization.
> >
> > dat<-structure(list(Sample = structure(c(1L, 12L, 13L, 14L, 15L, 16L,
> > 17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label =
> c("id1",
> > "id10", "id11", "id12", "id13", "id14", "id15", "id16", "id17",
> > "id18", "id19", "Id2", "id3", "id4", "id5", "id6", "id7", "id8",
> > "id9"), class = "factor"), Time1 = c(0L, 1L, 1L, 1L, 0L, 0L,
> > 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L), Time2 = c(1L,
> > 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L,
> > 1L, 1L)), .Names = c("Sample", "Time1", "Time2"), class = "data.frame",
> > row.names = c(NA,
> > -19L))
> > daT<-data.frame(dat %>%
> >mutate(Time1.but.not.in.Time2 = case_when(
> >  Time1 %in% "1" & Time2 %in% "0"  ~ "1"),
> > Time2.but.not.in.Time1 = case_when(
> >  Time1 %in% "0" & Time2 %in% "1"  ~ "1"),
> >   BothTimes = case_when(
> >  Time1 %in% "1" & Time2 %in% "1"  ~ "1")))
> >   daT
> >   summary(daT)
> >
> > cols.num <- c("Time1.but.not.in.Time2","Time2.but.not.in.Time1",
> > "BothTimes")
> > daT[cols.num] <- sapply(daT[cols.num],as.numeric)
> > summary(daT)
> > ProportionValue<-sum(daT$BothTimes, na.rm=T)/sum(daT$Time1, na.rm=T)
> > ProportionValue
> > standard error??
> >
> >   [[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-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to generate SE for the proportion value using a randomization process in R?

2021-01-22 Thread Marna Wagley
Hi All,
I was trying to estimate standard error (SE) for the proportion value using
some kind of randomization process (bootstrapping or jackknifing) in R, but
I could not figure it out.

Is there any way to generate SE for the proportion?

The example of the data and the code I am using is attached for your
reference. I would like to generate the value of proportion with a SE using
a 1000 times randomization.

dat<-structure(list(Sample = structure(c(1L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label = c("id1",
"id10", "id11", "id12", "id13", "id14", "id15", "id16", "id17",
"id18", "id19", "Id2", "id3", "id4", "id5", "id6", "id7", "id8",
"id9"), class = "factor"), Time1 = c(0L, 1L, 1L, 1L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L), Time2 = c(1L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L,
1L, 1L)), .Names = c("Sample", "Time1", "Time2"), class = "data.frame",
row.names = c(NA,
-19L))
daT<-data.frame(dat %>%
  mutate(Time1.but.not.in.Time2 = case_when(
Time1 %in% "1" & Time2 %in% "0"  ~ "1"),
Time2.but.not.in.Time1 = case_when(
Time1 %in% "0" & Time2 %in% "1"  ~ "1"),
 BothTimes = case_when(
Time1 %in% "1" & Time2 %in% "1"  ~ "1")))
 daT
 summary(daT)

cols.num <- c("Time1.but.not.in.Time2","Time2.but.not.in.Time1",
"BothTimes")
daT[cols.num] <- sapply(daT[cols.num],as.numeric)
summary(daT)
ProportionValue<-sum(daT$BothTimes, na.rm=T)/sum(daT$Time1, na.rm=T)
ProportionValue
standard error??

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Secondary y axis in ggplot2: did not respond when change its y-axis value

2021-01-08 Thread Marna Wagley
Thank you Rui and Thierry for the suggestion, it helped me.
thanks


On Fri, Jan 8, 2021 at 6:58 AM Rui Barradas  wrote:

> Hello,
>
> What about the following?
> First get the min and max of value by variable == "y1".
> Then use that range to scale up "y2".
>
> rng <- tapply(daT1$value, daT1$variable, range)$y1
>
> ggplot(data = daT1, aes(x = x, group = variable, color = variable)) +
>geom_line(data = subset(daT1, variable == "y1"),
>  aes(y = value)) +
>geom_line(data = subset(daT1, variable == "y2"),
>  aes(y = value*diff(rng) + rng[1])) +
>facet_wrap(~group) +
>scale_y_continuous(sec.axis = sec_axis(~ .*0.0001))
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 01:01 de 08/01/21, Marna Wagley escreveu:
> > Hi R users,
> > I was trying to plot a graph with a secondary axis, and used the
> following
> > code for the data but the secondary line and secondary y -axis value did
> > not match. I would like to show both lines in one graph.
> >
> > Any suggestions?
> >
> > library(ggplot2)
> > library(reshape2)
> > daT<-structure(list(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
> > 3L, 4L, 5L, 6L, 7L, 8L), y1 = c(9754L, 1051L, 5833L, 5769L, 2479L,
> > 470L, 5828L, 174L, 2045L, 6099L, 8780L, 8732L, 4053L, 9419L,
> > 4728L, 3587L), y2 = c(0.51, 0.61, 0.3, 0.81, 0.89, 0, 1.9, 0.76,
> > 0.87, 0.29, 0, 0.42, 0.73, 0.96, 0.62, 0.06), group = c("A",
> > "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B",
> > "B", "B")), class = "data.frame", row.names = c(NA, -16L))
> > print(daT)
> > daT1<-melt(daT, id.vars=c("x", "group"))
> > daT1%>%
> >ggplot() +
> >geom_line(aes(x = x, y = value, group = variable, color = variable)) +
> >facet_wrap(~group) +
> >scale_y_continuous(sec.axis = sec_axis(~ .*0.0001))
> >
> >   [[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-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Secondary y axis in ggplot2: did not respond when change its y-axis value

2021-01-07 Thread Marna Wagley
Hi R users,
I was trying to plot a graph with a secondary axis, and used the following
code for the data but the secondary line and secondary y -axis value did
not match. I would like to show both lines in one graph.

Any suggestions?

library(ggplot2)
library(reshape2)
daT<-structure(list(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L), y1 = c(9754L, 1051L, 5833L, 5769L, 2479L,
470L, 5828L, 174L, 2045L, 6099L, 8780L, 8732L, 4053L, 9419L,
4728L, 3587L), y2 = c(0.51, 0.61, 0.3, 0.81, 0.89, 0, 1.9, 0.76,
0.87, 0.29, 0, 0.42, 0.73, 0.96, 0.62, 0.06), group = c("A",
"A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B",
"B", "B")), class = "data.frame", row.names = c(NA, -16L))
print(daT)
daT1<-melt(daT, id.vars=c("x", "group"))
daT1%>%
  ggplot() +
  geom_line(aes(x = x, y = value, group = variable, color = variable)) +
  facet_wrap(~group) +
  scale_y_continuous(sec.axis = sec_axis(~ .*0.0001))

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to create a pivot table in r?

2020-02-14 Thread Marna Wagley
Dear Peter and Rui,
There are many dates (value) in some of the cells, If we want to chose only
one date (either oldest or newest) from that cell, how can we make a table
with that condition?
For example,
Using the following code;
M <- with(daT, tapply(as.character(ObsDate), list(id, ObsSite), list,
simplify=F))
data.frame(M)
  site1 site4 site5 site7
id1 NULL NULL NULL 6/13/13
id2 NULL NULL NULL 07/03/14,  05/17/14
id4 NULL 5/8/14 NULL NULL
id5 NULL NULL 6/13/14 NULL
id6 05/30/14, 06/28/13 NULL NULL NULL
id7 NULL NULL 6/25/13 NULL

If we want to take only one value (oldest date) if two or more dates/values
(classes) are found in the certain cells. For example would like to get the
following table (given below).
  site1 site4 site5 site7
id1 NULL NULL NULL 6/13/13
id2 NULL NULL NULL 07/03/14
id4 NULL 5/8/14 NULL NULL
id5 NULL NULL 6/13/14 NULL
id6 6/28/13 NULL NULL NULL
id7 NULL NULL 6/25/13 NULL

here is the code and an example data

daT<-structure(list(id = structure(c(1L, 2L, 2L, 3L, 4L, 5L, 5L, 6L
), .Label = c("id1", "id2", "id4", "id5", "id6", "id7"), class = "factor"),
ObsSite = structure(c(4L, 4L, 4L, 2L, 3L, 1L, 1L, 3L), .Label =
c("site1",
"site4", "site5", "site7"), class = "factor"), ObsDate =
structure(c(4L,
8L, 2L, 1L, 5L, 3L, 7L, 6L), .Label = c("05/08/14", "05/17/14",
"05/30/14", "06/13/13", "06/13/14", "06/25/13", "06/28/13",
"07/03/14"), class = "factor")), .Names = c("id", "ObsSite",
"ObsDate"), class = "data.frame", row.names = c(NA, -8L))
daT
daT$date <- mdy(daT$ObsDate)
tmp <- split(daT, daT$id)
head(tmp)
M <- with(daT, tapply(as.character(ObsDate), list(id, ObsSite), list,
simplify=F))
data.frame(M)
M[["id2", "site7"]]
# but I wanted to get the following table
daT2<-structure(list(X = structure(1:6, .Label = c("id1", "id2", "id4",
"id5", "id6", "id7"), class = "factor"), site1 = structure(c(2L,
2L, 2L, 2L, 1L, 2L), .Label = c("6/28/13", "NULL"), class = "factor"),
site4 = structure(c(2L, 2L, 1L, 2L, 2L, 2L), .Label = c("5/8/14",
"NULL"), class = "factor"), site5 = structure(c(3L, 3L, 3L,
1L, 3L, 2L), .Label = c("6/13/14", "6/25/13", "NULL"), class =
"factor"),
site7 = structure(c(2L, 1L, 3L, 3L, 3L, 3L), .Label = c("5/17/14",
"6/13/13", "NULL"), class = "factor")), .Names = c("X", "site1",
"site4", "site5", "site7"), class = "data.frame", row.names = c(NA,
-6L))
daT2
Thank you




On Thu, Feb 6, 2020 at 8:48 AM Marna Wagley  wrote:

> Thank You Profs. Dalgaard and Barradas for the code, both codes worked
> perfectly for the data and I am going to use it in my big data set.
> Thanks once again.
>
>
> On Thu, Feb 6, 2020 at 2:02 AM peter dalgaard  wrote:
>
>> There is also
>>
>> > with(daT, tapply(as.character(ObsDate), list(id, ObsSite),
>> function(x)format(list(x
>> site1site4  site5  site7
>> id1 NA   NA NA "06/13/13"
>> id2 NA   NA NA "07/03/14, 05/17/14"
>> id4 NA   "05/08/14" NA NA
>> id5 NA   NA "06/13/14" NA
>> id6 "05/30/14, 06/28/13" NA NA NA
>> id7 NA   NA "06/25/13" NA
>>
>> ...with the added bonus that if you leave out the format() business, you
>> get a data structure that doesn't print as nicely, but can be used for
>> further computations:
>>
>> > with(daT, tapply(as.character(ObsDate), list(id, ObsSite), list,
>> simplify=FALSE))
>> site1  site4  site5  site7
>> id1 NULL   NULL   NULL   List,1
>> id2 NULL   NULL   NULL   List,1
>> id4 NULL   List,1 NULL   NULL
>> id5 NULL   NULL   List,1 NULL
>> id6 List,1 NULL   NULL   NULL
>> id7 NULL   NULL   List,1 NULL
>> > M <- with(daT, tapply(as.character(ObsDate), list(id, ObsSite), list,
>> simplify=FALSE))
>> > M[["id2", "site7"]]
>> [[1]]
>> [1] "07/03/14" "05/17/14"
>>
>> -pd
>>
>> > On 6 Feb 2020, at 01:37 , Marna Wagley  wrote:
>> >
>> > daT<-structure(list(id = structure(c(1L, 2L, 2L, 3L, 4L, 5L, 5L, 6L
>> > ), .Label = c("id1", "id2", "id4"

Re: [R] how to create a pivot table in r?

2020-02-06 Thread Marna Wagley
Thank You Profs. Dalgaard and Barradas for the code, both codes worked
perfectly for the data and I am going to use it in my big data set.
Thanks once again.


On Thu, Feb 6, 2020 at 2:02 AM peter dalgaard  wrote:

> There is also
>
> > with(daT, tapply(as.character(ObsDate), list(id, ObsSite),
> function(x)format(list(x
> site1site4  site5  site7
> id1 NA   NA NA "06/13/13"
> id2 NA   NA NA "07/03/14, 05/17/14"
> id4 NA   "05/08/14" NA NA
> id5 NA   NA "06/13/14" NA
> id6 "05/30/14, 06/28/13" NA NA NA
> id7 NA   NA "06/25/13" NA
>
> ...with the added bonus that if you leave out the format() business, you
> get a data structure that doesn't print as nicely, but can be used for
> further computations:
>
> > with(daT, tapply(as.character(ObsDate), list(id, ObsSite), list,
> simplify=FALSE))
> site1  site4  site5  site7
> id1 NULL   NULL   NULL   List,1
> id2 NULL   NULL   NULL   List,1
> id4 NULL   List,1 NULL   NULL
> id5 NULL   NULL   List,1 NULL
> id6 List,1 NULL   NULL   NULL
> id7 NULL   NULL   List,1 NULL
> > M <- with(daT, tapply(as.character(ObsDate), list(id, ObsSite), list,
> simplify=FALSE))
> > M[["id2", "site7"]]
> [[1]]
> [1] "07/03/14" "05/17/14"
>
> -pd
>
> > On 6 Feb 2020, at 01:37 , Marna Wagley  wrote:
> >
> > daT<-structure(list(id = structure(c(1L, 2L, 2L, 3L, 4L, 5L, 5L, 6L
> > ), .Label = c("id1", "id2", "id4", "id5", "id6", "id7"), class =
> "factor"),
> >ObsSite = structure(c(4L, 4L, 4L, 2L, 3L, 1L, 1L, 3L), .Label =
> > c("site1",
> >"site4", "site5", "site7"), class = "factor"), ObsDate =
> > structure(c(4L,
> >8L, 2L, 1L, 5L, 3L, 7L, 6L), .Label = c("05/08/14", "05/17/14",
> >"05/30/14", "06/13/13", "06/13/14", "06/25/13", "06/28/13",
> >"07/03/14"), class = "factor")), .Names = c("id", "ObsSite",
> > "ObsDate"), class = "data.frame", row.names = c(NA, -8L))
> > daT
> > daT$date <- mdy(daT$ObsDate)
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd@cbs.dk  Priv: pda...@gmail.com
>
>
>
>
>
>
>
>
>
>

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] how to create a pivot table in r?

2020-02-05 Thread Marna Wagley
Hi R users,
I was trying to create a pivot table for the following data, in which I
wanted to put "id" in  rows and "ObsSite" in columns and "Obsdate" is in
the cells.

I used the following code but it took only one date among the two dates.
For example, the animal (Id2) which was observed in the site7  two time or
days (07/03/14 & 05/17/2014). see below
id ObsSite ObsDate
id1 site7 06/13/13
id2 site7 07/03/14
id2 site7 05/17/14
id4 site4 05/08/14
id5 site5 06/13/14
id6 site1 05/30/14
id6 site1 06/28/13
id7 site5 06/25/13

I wanted to put both dates in the cell if there is any multiple dates, as
similar shown below





  site1 site4 site5 site7
id1 0 0 0 6/13/13
id2 0 0 0 7/3/2014, 5/17/2014
id4 0 5/8/14 0 0
id5 0 0 6/13/14 0
id6 5/30/2014, 6/28/2013 0 0 0
id7 0 0 6/25/13 0

the code I used is given below but it gave me only one date in that cells.
Is there any way to get both dates in these cells?
Thanks,

###
library(lubridate)
daT<-structure(list(id = structure(c(1L, 2L, 2L, 3L, 4L, 5L, 5L, 6L
), .Label = c("id1", "id2", "id4", "id5", "id6", "id7"), class = "factor"),
ObsSite = structure(c(4L, 4L, 4L, 2L, 3L, 1L, 1L, 3L), .Label =
c("site1",
"site4", "site5", "site7"), class = "factor"), ObsDate =
structure(c(4L,
8L, 2L, 1L, 5L, 3L, 7L, 6L), .Label = c("05/08/14", "05/17/14",
"05/30/14", "06/13/13", "06/13/14", "06/25/13", "06/28/13",
"07/03/14"), class = "factor")), .Names = c("id", "ObsSite",
"ObsDate"), class = "data.frame", row.names = c(NA, -8L))
daT
daT$date <- mdy(daT$ObsDate)
tmp <- split(daT, daT$id)
head(tmp)

pivotTable <- do.call(rbind, lapply(tmp, function(daT){
  tb <- table(daT$ObsSite)
  idx <- which(tb>0)
  tb1 <- replace(tb, idx, as.character(daT$date))
}))


data.frame(pivotTable)

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] how to split a column by tab ?

2019-09-09 Thread Marna Wagley
Hi R User,
I was trying to split a column by tabs, I tried to split with several ways,
but I could not split it. Is there any possibilities?

The data example and the code I used
daT1<-c("Column number 12345678
 9   10   10   10   10   10   10   10",
"comes from position   172635   15948
10   11   12   13   14   16"
)
daT2 <- data.frame(do.call('rbind',
strsplit(as.character(daT1),'\t',fixed=T)))
colnames(daT2)
daT2

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] creating a line plot for time series data for several years

2019-09-05 Thread Marna Wagley
Thank you, Rui. It helped me a lot. It is highly appreciated.
thanks,

On Thu, Sep 5, 2019 at 1:14 AM Rui Barradas  wrote:

> Hello,
>
> Thanks for the reproducible example.
> All you have to do is to use the aesthetic group = year in either
> ggplot() or geom_line().
>
> This works:
>
>
> library(ggplot2)
>
> daT$date <- as.Date(daT$date)
> daT$DATE <- format(daT$date, format="%m/%d")
>
> Ab <- ggplot(daT, aes(x = as.factor(DATE),
>y = abundance, colour = site)) +
>geom_line(aes(group = year)) +
>theme_bw()
>
> Ab + facet_grid(year ~ .) +
>theme(axis.text.x = element_text(angle = 90,
> hjust = 1,
>     size = 4))
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 21:37 de 04/09/19, Marna Wagley escreveu:
> > Hi R Users,
> > I have been getting a trouble to create a time series plot (line) as I
> was
> > trying to create a line graph for each year using month and date (in x
> > axis). I would like to put only one x axis (month and date) for three
> years
> > using the facet_grid, but it has not been creating a lineplot for me. I
> > wanted to compare (visually) whether  the date of observing of the
> > population vary by year.
> > I used the following code but did not plot for me any line. Is there any
> > possibility to create the lines for each year? A sample of the data is
> > given for your information.
> >
> > daT$DATE<-format(as.Date(daT$date), format="%m/%d")
> > Ab<-ggplot(daT, aes(x= as.factor(DATE), y=abundance, colour=site)) +
> >  geom_line() +
> >  theme_bw()
> > Ab+facet_grid(year~.)+theme(axis.text.x = element_text(angle = 90, hjust
> =
> > 1, size=4))
> > ###
> >
> > daT<-structure(list(year = c(2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
> > 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
> > 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
> > 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
> > 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
> > 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
> > 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
> > 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
> > 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
> > 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
> > 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
> > 2018L, 2018L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
> > 2017L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
> > 2016L, 2016L, 2016

[R] creating a line plot for time series data for several years

2019-09-04 Thread Marna Wagley
Hi R Users,
I have been getting a trouble to create a time series plot (line) as I was
trying to create a line graph for each year using month and date (in x
axis). I would like to put only one x axis (month and date) for three years
using the facet_grid, but it has not been creating a lineplot for me. I
wanted to compare (visually) whether  the date of observing of the
population vary by year.
I used the following code but did not plot for me any line. Is there any
possibility to create the lines for each year? A sample of the data is
given for your information.

daT$DATE<-format(as.Date(daT$date), format="%m/%d")
Ab<-ggplot(daT, aes(x= as.factor(DATE), y=abundance, colour=site)) +
geom_line() +
theme_bw()
Ab+facet_grid(year~.)+theme(axis.text.x = element_text(angle = 90, hjust =
1, size=4))
###

daT<-structure(list(year = c(2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L), date = structure(c(231L, 232L, 233L, 234L, 235L, 236L,
237L, 238L, 239L, 240L, 241L, 242L, 243L, 244L, 245L, 246L, 247L,
248L, 249L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 257L, 258L,
259L, 260L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L, 269L,
270L, 271L, 272L, 273L, 274L, 275L, 276L, 277L, 278L, 279L, 280L,
281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L, 289L, 290L, 291L,
292L, 293L, 294L, 295L, 296L, 297L, 298L, 299L, 300L, 301L, 302L,
303L, 304L, 305L, 306L, 307L, 308L, 309L, 310L, 311L, 312L, 313L,
314L, 315L, 316L, 317L, 318L, 319L, 320L, 321L, 322L, 323L, 324L,
325L, 326L, 327L, 328L, 118L, 119L, 120L, 121L, 122L, 123L, 124L,
125L, 126L, 127L, 128L, 129L, 130L, 131L, 131L, 132L, 132L, 133L,
133L, 134L, 135L, 135L, 136L, 136L, 137L, 138L, 138L, 139L, 140L,
141L, 142L, 143L, 144L, 145L, 146L, 146L, 147L, 148L, 149L, 150L,
151L, 152L, 153L, 154L, 155L, 156L, 157L, 157L, 158L, 158L, 159L,
159L, 160L, 160L, 161L, 162L, 163L, 164L, 164L, 165L, 165L, 166L,
166L, 167L, 167L, 168L, 168L, 169L, 170L, 170L, 171L, 171L, 172L,
172L, 173L, 173L, 174L, 174L, 175L, 175L, 176L, 176L, 177L, 178L,
178L, 179L, 179L, 180L, 180L, 181L, 182L, 183L, 183L, 184L, 184L,
185L, 186L, 187L, 188L, 188L, 189L, 190L, 190L, 191L, 191L, 192L,
193L, 194L, 195L, 195L, 196L, 196L, 197L, 197L, 198L, 198L, 199L,
199L, 200L, 201L, 

Re: [R] How to create Pivot table in r?

2019-07-05 Thread Marna Wagley
Hi Rui and Richard,
Thank you very much for the help. You are very smart and your code saved my
time tremendously.
I added one line and got the results what I was looking for it.

After "agg", I added dcast function


agg <- aggregate(as.character(ID) ~ site2 + site1, daT1, paste, sep = ", ")

names(agg)[3] <- "ID"

agg


agg_wide<- dcast(agg, site2~ site1, value.var="ID")
agg_wide


On Fri, Jul 5, 2019 at 10:55 AM Richard M. Heiberger  wrote:

> daT1 <- structure(list(ID = structure(c(13L, 11L, 18L, 8L, 12L, 7L, 15L,
> 2L, 1L, 17L, 16L, 14L, 10L, 9L, 6L, 3L, 5L, 4L), .Label = c("1B82",
> "7A1D", "848A8", "8737SP", "87E6E", "941FD", "942C", "9C39",
> "9D10B", "A1271", "A253", "A451", "C27A", "C767D", "CBA8", "CDF10",
> "D5DB", "FB3E"), class = "factor"), site1 = structure(c(3L, 1L,
> 2L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L
> ), .Label = c("3-Mar-2015", "30-Mar-2015", "31-Mar-2015"), class = "factor"
> ),
> site2 = structure(c(1L, 3L, 1L, 1L, 6L, 1L, 6L, 1L, 2L, 5L,
> 5L, 1L, 1L, 1L, 4L, 1L, 1L, 4L), .Label = c("", "14-Apr-2015",
> "17-Apr-2015", "2-May-2015", "22-Apr-2015", "9-Mar-2015"), class =
> "factor")), .Names = c("ID",
> "site1", "site2"), class = "data.frame", row.names = c(NA, -18L
> ))
>
> daT1
> sapply(daT1, class) ## factors interfere with what you want.
>
> ## make ID "character" and the two date columns "Date"
> daT2 <- data.frame(ID=as.character(daT1$ID),
>site1=as.Date(daT1$site1, format = "%d-%b-%Y"),
>site2=as.Date(daT1$site2, format = "%d-%b-%Y"),
>stringsAsFactors=FALSE)
> sapply(daT2, class)
>
> result <- tapply(daT2$ID, daT2[,2:3], c)
> result
> result[3,4:5]
>
> On Fri, Jul 5, 2019 at 12:48 PM Marna Wagley 
> wrote:
> >
> > Hi Rui,
> > Thank you very much for the code. It is great as it gave the number of
> > individuals encountered in each cell but- is it possible to see which
> > individuals were in that cell?
> > for example - site2 (2015-05-02) and site2(2015-03-31) has value 2, is it
> > possible to show these two  "941FD","8737SP" IDs in that cell? It must be
> > hard to create this type of table.
> >
> > daT1<-structure(list(ID = structure(c(13L, 11L, 18L, 8L, 12L, 7L, 15L,
> >
> > 2L, 1L, 17L, 16L, 14L, 10L, 9L, 6L, 3L, 5L, 4L), .Label = c("1B82",
> >
> > "7A1D", "848A8", "8737SP", "87E6E", "941FD", "942C", "9C39",
> >
> > "9D10B", "A1271", "A253", "A451", "C27A", "C767D", "CBA8", "CDF10",
> >
> > "D5DB", "FB3E"), class = "factor"), site1 = structure(c(3L, 1L,
> >
> > 2L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L
> >
> > ), .Label = c("3-Mar-2015", "30-Mar-2015", "31-Mar-2015"), class =
> "factor"
> > ),
> >
> > site2 = structure(c(1L, 3L, 1L, 1L, 6L, 1L, 6L, 1L, 2L, 5L,
> >
> > 5L, 1L, 1L, 1L, 4L, 1L, 1L, 4L), .Label = c("", "14-Apr-2015",
> >
> > "17-Apr-2015", "2-May-2015", "22-Apr-2015", "9-Mar-2015"), class =
> > "factor")), .Names = c("ID",
> >
> > "site1", "site2"), class = "data.frame", row.names = c(NA, -18L
> >
> > ))
> >
> >
> > daT1$site1 <- as.Date(daT1$site1, format = "%d-%b-%Y")
> >
> > daT1$site2 <- as.Date(daT1$site2, format = "%d-%b-%Y")
> >
> > AA<-xtabs(~ site2 + site1, daT1)
> >
> > AA
> >
> >
> > Once again, I am very grateful to you.
> >
> > Thanks
> >
> >
> >
> > On Fri, Jul 5, 2019 at 1:57 AM  wrote:
> >
> > > Hello,
> > >
> > > Maybe you want to take a look at function xtabs.
> > >
> > > xtabs(~ site2 + site1, daT1)
> > >
> > > Note that the results are different if you convert to class "Date"
> first.
> > >
> > > daT1$site1 <- as.Date(daT1$site1, format = "%d-%b-%Y")
> > > daT1$site2 <

Re: [R] How to create Pivot table in r?

2019-07-05 Thread Marna Wagley
Hi Rui,
Thank you very much for the code. It is great as it gave the number of
individuals encountered in each cell but- is it possible to see which
individuals were in that cell?
for example - site2 (2015-05-02) and site2(2015-03-31) has value 2, is it
possible to show these two  "941FD","8737SP" IDs in that cell? It must be
hard to create this type of table.

daT1<-structure(list(ID = structure(c(13L, 11L, 18L, 8L, 12L, 7L, 15L,

2L, 1L, 17L, 16L, 14L, 10L, 9L, 6L, 3L, 5L, 4L), .Label = c("1B82",

"7A1D", "848A8", "8737SP", "87E6E", "941FD", "942C", "9C39",

"9D10B", "A1271", "A253", "A451", "C27A", "C767D", "CBA8", "CDF10",

"D5DB", "FB3E"), class = "factor"), site1 = structure(c(3L, 1L,

2L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L

), .Label = c("3-Mar-2015", "30-Mar-2015", "31-Mar-2015"), class = "factor"
),

site2 = structure(c(1L, 3L, 1L, 1L, 6L, 1L, 6L, 1L, 2L, 5L,

5L, 1L, 1L, 1L, 4L, 1L, 1L, 4L), .Label = c("", "14-Apr-2015",

"17-Apr-2015", "2-May-2015", "22-Apr-2015", "9-Mar-2015"), class =
"factor")), .Names = c("ID",

"site1", "site2"), class = "data.frame", row.names = c(NA, -18L

))


daT1$site1 <- as.Date(daT1$site1, format = "%d-%b-%Y")

daT1$site2 <- as.Date(daT1$site2, format = "%d-%b-%Y")

AA<-xtabs(~ site2 + site1, daT1)

AA


Once again, I am very grateful to you.

Thanks



On Fri, Jul 5, 2019 at 1:57 AM  wrote:

> Hello,
>
> Maybe you want to take a look at function xtabs.
>
> xtabs(~ site2 + site1, daT1)
>
> Note that the results are different if you convert to class "Date" first.
>
> daT1$site1 <- as.Date(daT1$site1, format = "%d-%b-%Y")
> daT1$site2 <- as.Date(daT1$site2, format = "%d-%b-%Y")
> xtabs(~ site2 + site1, daT1)
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Citando Marna Wagley :
>
> > Hi R users,
> > I was trying  to create a pivot table (cross tabulated) by Site1date
> (rows)
> > and Site2 date (columns), and spent substantial time but no luck to
> create
> > it. Is there any possibility to create it?
> > I would be very grateful to your help.
> >
> > "daT1" is row data, in which three columns (speciesID, detected at SiteA,
> > Site B) and cell has date in which they were detected. From this data, I
> > wanted to create a table to show a joint detection between site1 and
> Site2.
> >
> > I was trying to get the table similar to "daT2", which is a cross
> tabulated
> > table by Site1 date (rows), and Site2 date (columns). After that I would
> > like to see another table related sum of the detecttions in each cell (
> > "daT3" table)
> >
> > The example data are given below (daT1) and output tables (daT2, daT3). I
> > appreciate your help very much.
> >
> > Sincerely
> >
> >
> >
> > daT1<-structure(list(ID = structure(c(13L, 11L, 18L, 8L, 12L, 7L, 15L,
> > 2L, 1L, 17L, 16L, 14L, 10L, 9L, 6L, 3L, 5L, 4L), .Label = c("1B82",
> > "7A1D", "848A8", "8737SP", "87E6E", "941FD", "942C", "9C39",
> > "9D10B", "A1271", "A253", "A451", "C27A", "C767D", "CBA8", "CDF10",
> > "D5DB", "FB3E"), class = "factor"), site1 = structure(c(3L, 1L,
> > 2L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L
> > ), .Label = c("3-Mar-2015", "30-Mar-2015", "31-Mar-2015"), class =
> > "factor"),
> > site2 = structure(c(1L, 3L, 1L, 1L, 6L, 1L, 6L, 1L, 2L, 5L,
> > 5L, 1L, 1L, 1L, 4L, 1L, 1L, 4L), .Label = c("", "14-Apr-2015",
> > "17-Apr-2015", "2-May-2015", "22-Apr-2015", "9-Mar-2015"), class =
> > "factor")), .Names = c("ID",
> > "site1", "site2"), class = "data.frame", row.names = c(NA, -18L
> > ))
> >
> > daT2<-structure(list(Date = structure(c(1L, 12L, 23L, 26L, 27L, 28L,
> > 29L, 30L, 31L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L,
> > 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 32L, 43L,
> > 54L, 56L, 57L, 58L, 59L, 60L, 61L, 33L, 34L, 35L, 36L, 37L, 38L,
> > 39L, 40L, 41L, 42L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 5

[R] How to create Pivot table in r?

2019-07-05 Thread Marna Wagley
Hi R users,
I was trying  to create a pivot table (cross tabulated) by Site1date (rows)
and Site2 date (columns), and spent substantial time but no luck to create
it. Is there any possibility to create it?
I would be very grateful to your help.

"daT1" is row data, in which three columns (speciesID, detected at SiteA,
Site B) and cell has date in which they were detected. From this data, I
wanted to create a table to show a joint detection between site1 and Site2.

I was trying to get the table similar to "daT2", which is a cross tabulated
table by Site1 date (rows), and Site2 date (columns). After that I would
like to see another table related sum of the detecttions in each cell (
"daT3" table)

The example data are given below (daT1) and output tables (daT2, daT3). I
appreciate your help very much.

Sincerely



daT1<-structure(list(ID = structure(c(13L, 11L, 18L, 8L, 12L, 7L, 15L,
2L, 1L, 17L, 16L, 14L, 10L, 9L, 6L, 3L, 5L, 4L), .Label = c("1B82",
"7A1D", "848A8", "8737SP", "87E6E", "941FD", "942C", "9C39",
"9D10B", "A1271", "A253", "A451", "C27A", "C767D", "CBA8", "CDF10",
"D5DB", "FB3E"), class = "factor"), site1 = structure(c(3L, 1L,
2L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L
), .Label = c("3-Mar-2015", "30-Mar-2015", "31-Mar-2015"), class =
"factor"),
site2 = structure(c(1L, 3L, 1L, 1L, 6L, 1L, 6L, 1L, 2L, 5L,
5L, 1L, 1L, 1L, 4L, 1L, 1L, 4L), .Label = c("", "14-Apr-2015",
"17-Apr-2015", "2-May-2015", "22-Apr-2015", "9-Mar-2015"), class =
"factor")), .Names = c("ID",
"site1", "site2"), class = "data.frame", row.names = c(NA, -18L
))

daT2<-structure(list(Date = structure(c(1L, 12L, 23L, 26L, 27L, 28L,
29L, 30L, 31L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 32L, 43L,
54L, 56L, 57L, 58L, 59L, 60L, 61L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L,
53L, 55L, 62L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 63L), .Label =
c("3/1/2015",
"3/10/2015", "3/11/2015", "3/12/2015", "3/13/2015", "3/14/2015",
"3/15/2015", "3/16/2015", "3/17/2015", "3/18/2015", "3/19/2015",
"3/2/2015", "3/20/2015", "3/21/2015", "3/22/2015", "3/23/2015",
"3/24/2015", "3/25/2015", "3/26/2015", "3/27/2015", "3/28/2015",
"3/29/2015", "3/3/2015", "3/30/2015", "3/31/2015", "3/4/2015",
"3/5/2015", "3/6/2015", "3/7/2015", "3/8/2015", "3/9/2015", "4/1/2015",
"4/10/2015", "4/11/2015", "4/12/2015", "4/13/2015", "4/14/2015",
"4/15/2015", "4/16/2015", "4/17/2015", "4/18/2015", "4/19/2015",
"4/2/2015", "4/20/2015", "4/21/2015", "4/22/2015", "4/23/2015",
"4/24/2015", "4/25/2015", "4/26/2015", "4/27/2015", "4/28/2015",
"4/29/2015", "4/3/2015", "4/30/2015", "4/4/2015", "4/5/2015",
"4/6/2015", "4/7/2015", "4/8/2015", "4/9/2015", "5/1/2015", "5/10/2015",
"5/2/2015", "5/3/2015", "5/4/2015", "5/5/2015", "5/6/2015", "5/7/2015",
"5/8/2015", "5/9/2015"), class = "factor"), X3.1.2015 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), X3.2.2015 = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), X3.3.2015 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), X3.4.2015 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), X3.5.2015 = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), X3.6.2015 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), X3.7.2015 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 

[R] create a table for time difference (from t3 to t1, so on..)

2019-05-03 Thread Marna Wagley
Hi R User.
I have a date set in which I wanted to find the duration (period)  between
released time and detection time for each individual. Is there any simplest
way to create a matrix?
I appreciate your help.

Thanks,
MW
--
Here is the example data,

dAT<-structure(list(Id = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 5L, 4L,

1L, 6L, 6L, 3L, 4L, 1L), .Label = c("a", "b", "c", "e", "f",

"m"), class = "factor"), date = structure(c(1L, 1L, 1L, 5L, 4L,

1L, 5L, 5L, 3L, 1L, 2L, 7L, 6L, 8L), .Label = c("1-Jan-18", "1-Jul-18",

"10-Mar-18", "15-Jan-18", "2-Jan-18", "27-Jan-18", "3-Jan-18",

"7-Mar-19"), class = "factor")), .Names = c("Id", "date"), class =
"data.frame", row.names = c(NA,

-14L))


#and the results looks like


Result<-structure(list(Id = structure(1:6, .Label = c("a", "b", "c",

"e", "f", "m"), class = "factor"), intial.released.date = structure(c(1L,

1L, 1L, 2L, 2L, 1L), .Label = c("1-Jan-18", "2-Jan-18"), class = "factor"),

duration.between.t2.and.t1 = c(14L, 0L, 2L, 0L, NA, 181L),

duration.between.t3.and.t1 = c(68L, NA, NA, 25L, NA, NA),

duration.between.t4.and.t1 = c(430L, NA, NA, NA, NA, NA),

duration.between.t3.and.t2 = c(54L, NA, NA, NA, NA, NA),

duration.between.t4.and.t2 = c(416L, NA, NA, NA, NA, NA)), .Names = c(
"Id",

"intial.released.date", "duration.between.t2.and.t1",
"duration.between.t3.and.t1",

"duration.between.t4.and.t1", "duration.between.t3.and.t2",
"duration.between.t4.and.t2"

), class = "data.frame", row.names = c(NA, -6L))

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Monte Carlo simulation for ratio and its CI

2019-03-26 Thread Marna Wagley
Dear Bert,
Thank you very much for the response.
I did it manually but I could not put them in a loop so that I created the
table manually with selecting the rows randomly several times. Here what I
have done so far, please find it. I want to create the table 100 times and
calculate its mean and CI from those 100 values. If anyone can give me some
hint to make a loop, that would be great. I am very grateful with your help.
Thanks,

library(dplyr)

library(plyr)

dat<-structure(list(v1 = c(NA, TRUE, TRUE, TRUE, TRUE, TRUE, NA, TRUE,

NA, NA, TRUE, TRUE, TRUE, TRUE, NA, NA, TRUE, TRUE), v2 = c(TRUE,

NA, NA, NA, NA, TRUE, NA, NA, TRUE, TRUE, NA, TRUE, TRUE, NA,

NA, TRUE, TRUE, NA), v3 = c(TRUE, TRUE, NA, TRUE, TRUE, NA, NA,

TRUE, TRUE, NA, NA, TRUE, TRUE, TRUE, NA, NA, TRUE, NA)), .Names = c("v1",

"v2", "v3"), class = "data.frame", row.names = c(NA, -18L))


ratio1 <- with(dat, sum(v1,na.rm = TRUE)/sum(v3,na.rm=TRUE))

ratio2 <- with(dat, sum(v2,na.rm = TRUE)/sum(v3,na.rm=TRUE))

#

A1<-sample_n(dat1, 16)# created a table with selecting a 16 sample size
(rows)

A1.ratio1<-with(A1, sum(v1,na.rm = TRUE)/sum(v3,na.rm=TRUE))

A1.ratio2 <- with(A1, sum(v2,na.rm = TRUE)/sum(v3,na.rm=TRUE))

A1.Table<-data.frame(Ratio1=A1.ratio1, Ratio2=A1.ratio2)

#

A2<-sample_n(dat1, 16)

A2.ratio1<-with(A2, sum(v1,na.rm = TRUE)/sum(v3,na.rm=TRUE))

A2.ratio2 <- with(A2, sum(v2,na.rm = TRUE)/sum(v3,na.rm=TRUE))

A2.Table<-data.frame(Ratio1=A2.ratio1, Ratio2=A2.ratio2)

#

A3<-sample_n(dat1, 16)

A3.ratio1<-with(A3, sum(v1,na.rm = TRUE)/sum(v3,na.rm=TRUE))

A3.ratio2 <- with(A3, sum(v2,na.rm = TRUE)/sum(v3,na.rm=TRUE))

A3.Table<-data.frame(Ratio1=A3.ratio1, Ratio2=A3.ratio2)

#

##..

# I was thinking to repeat this procedure 100 times and calculate the ratio

A100<-sample_n(dat1, 16)

A100.ratio1<-with(A100, sum(v1,na.rm = TRUE)/sum(v3,na.rm=TRUE))

A100.ratio2 <- with(A100, sum(v2,na.rm = TRUE)/sum(v3,na.rm=TRUE))

A100.Table<-data.frame(Ratio1=A100.ratio1, Ratio2=A100.ratio2)

#

Tab<-rbind(A1.Table, A2.Table, A3.Table, A100.Table)


#Compute the mean for each ratio

Ratio1<-mean(Table1[,1])

Ratio2<-mean(Table1[,2])


summary <- ddply(subset(Tab), c(""),summarise,

   N= length(Tab),

   mean.R1 = mean(Ratio1, na.rm=T),

   median.R1=median(Ratio1, na.rm=T),

   sd.R1   = sd(Ratio1, na.rm=T),

   se.R1   = sd / sqrt(N),

   LCI.95.R1=mean.R1-1.95*se.R1,

   UCI.95.R1=mean.R1+1.95*se.R1,



   mean.R2 = mean(Ratio2, na.rm=T),

   median.R2=median(Ratio2, na.rm=T),

   sd.R2   = sd(Ratio2, na.rm=T),

   se.R2   = sd / sqrt(N),

   LCI.95.R2=mean.R2-1.95*se.R2,

   UCI.95.R2=mean.R2+1.95*se.R2

   )

 summary



On Mon, Mar 25, 2019 at 4:50 PM Bert Gunter  wrote:

>
> > ratio1 <- with(dat, sum(v1,na.rm = TRUE)/sum(v3,na.rm=TRUE))
> > ratio1
> [1] 1.2
>
> It looks like you should spend some more time with an R tutorial or two.
> This is basic stuff (if I understand what you wanted correctly).
>
> Also, this is not how a "confidence interval" should be calculated, but
> that is another off topic discussion for which stats.stackexchange.com is
> a more appropriate venue.
>
> Cheers,
> Bert
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Mon, Mar 25, 2019 at 4:31 PM Marna Wagley 
> wrote:
>
>> Hi R User,
>> I was trying to calculate ratios with confidence interval using Monte
>> Carlo
>> simulation but I could not figure it out.
>> Here is the example of my data (see below), I want to calculate ratios
>> (dat$v1/dat$v3 & dat$v2/dat$v3) and its confidence intervals using a 100
>> randomly selected data sets.
>> Could you please give me your suggestions how I can estimate ratios with
>> CI?
>> I will be very grateful to you.
>> Sincerely,
>>
>> MW
>> ---
>> dat<-structure(list(v1 = c(NA, TRUE, TRUE, TRUE, TRUE, TRUE, NA, TRUE,
>>
>> NA, NA, TRUE, TRUE, TRUE, TRUE, NA, NA, TRUE, TRUE), v2 = c(TRUE,
>>
>> NA, NA, NA, NA, TRUE, NA, NA, TRUE, TRUE, NA, TRUE, TRUE, NA,
>>
>> NA, TRUE, TRUE, NA), v3 = c(TRUE, TRUE, NA, TRUE, TRUE, NA, NA,
>>
>> TRUE, TRUE, NA, NA, TRUE, TRUE, TRUE, NA, NA, TRUE, NA)), .Names = c("v1",
>>
>> "v2", "v3"), class = "data.frame", row.names = c(NA, -18L))
>>
>>
>> ratio1<-length(which(dat$v1 == "TRUE"))/length(which(dat$v

[R] Monte Carlo simulation for ratio and its CI

2019-03-25 Thread Marna Wagley
Hi R User,
I was trying to calculate ratios with confidence interval using Monte Carlo
simulation but I could not figure it out.
Here is the example of my data (see below), I want to calculate ratios
(dat$v1/dat$v3 & dat$v2/dat$v3) and its confidence intervals using a 100
randomly selected data sets.
Could you please give me your suggestions how I can estimate ratios with
CI?
I will be very grateful to you.
Sincerely,

MW
---
dat<-structure(list(v1 = c(NA, TRUE, TRUE, TRUE, TRUE, TRUE, NA, TRUE,

NA, NA, TRUE, TRUE, TRUE, TRUE, NA, NA, TRUE, TRUE), v2 = c(TRUE,

NA, NA, NA, NA, TRUE, NA, NA, TRUE, TRUE, NA, TRUE, TRUE, NA,

NA, TRUE, TRUE, NA), v3 = c(TRUE, TRUE, NA, TRUE, TRUE, NA, NA,

TRUE, TRUE, NA, NA, TRUE, TRUE, TRUE, NA, NA, TRUE, NA)), .Names = c("v1",

"v2", "v3"), class = "data.frame", row.names = c(NA, -18L))


ratio1<-length(which(dat$v1 == "TRUE"))/length(which(dat$v3 == "TRUE"))

ratio2<-length(which(dat$v2 == "TRUE"))/length(which(dat$v3 == "TRUE"))


Thanks

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] loop for comparing two or more groups using bootstrapping

2018-09-11 Thread Marna Wagley
Thank you Jim, it worked. I am very grateful for your help.
Thanks
KG

On Tue, Sep 11, 2018 at 3:51 PM Jim Lemon  wrote:

> Hi Kristi,
> My fault, I only worked out how to assign the values to the names and pick
> out the columns of daT for the calculations. I think this does what you
> want, but I can't guarantee the result.
>
> daT<-structure(list(year1=c(0.417,0.538,0.69,0.688,0.688,0.606,
> 0.667,0.7,0.545,0.462,0.711,0.642,0.744,0.604,0.612,
> 0.667,0.533,0.556,0.444,0.526,0.323,0.308,0.195,0.333,
> 0.323,0.256,0.345,0.205,0.286,0.706,0.7,0.6,0.571,0.364,
> 0.429,0.326,0.571,0.424,0.341,0.387,0.341,0.324,0.696,
> 0.696,0.583,0.556,0.645,0.435,0.471,0.556),year2=c(0.385,
> 0.552,0.645,0.516,0.629,0.595,0.72,0.638,0.557,0.588,
> 0.63,0.744,0.773,0.571,0.723,0.769,0.667,0.667,0.526,
> 0.476,0.294,0.323,0.222,0.556,0.263,0.37,0.357,0.25,
> 0.323,0.778,0.667,0.636,0.583,0.432,0.412,0.333,0.571,
> 0.39,0.4,0.452,0.326,0.471,0.7,0.75,0.615,0.462,0.556,
> 0.4,0.696,0.465),year3=c(0.435,0.759,0.759,0.759,0.714,
> 0.593,0.651,0.683,0.513,0.643,0.652,0.757,0.791,0.649,
> 0.78,0.5,0.5,0.5,0.533,0.429,0.333,0.286,0.231,0.533,
> 0.303,0.417,0.333,0.333,0.357,0.909,1,0.952,0.8,0.556,
> 0.529,0.562,0.762,0.513,0.733,0.611,0.733,0.647,0.909,
> 0.857,0.8,0.556,0.588,0.562,0.857,0.513),year4=c(0.333,
> 0.533,0.6,0.483,0.743,0.5,0.691,0.619,0.583,0.385,0.653,
> 0.762,0.844,0.64,0.667,0.571,0.571,0.615,0.421,0.5,0.205,
> 0.308,0.25,0.6,0.242,0.308,0.276,0.235,0.211,0.9,0.632,
> 0.72,0.727,0.356,0.5,0.368,0.5,0.41,0.562,0.514,0.4,
> 0.409,0.632,0.72,0.727,0.4,0.5,0.421,0.5,0.462)),.Names=c("year1",
> "year2","year3","year4"),row.names=c(NA,-50L),class="data.frame")
> colname.mat<-combn(paste0("year",1:4),2)
> samplenames<-apply(colname.mat,2,paste,collapse="")
> k<-1
> meandiff<-function(x) return(mean(x[[1]])-mean(x[[2]]))
> for(column in 1:ncol(colname.mat)) {
>  assign(samplenames[column],
>   replicate(k,data.frame(sample(daT[,colname.mat[1,column]],3,TRUE),
>sample(daT[,colname.mat[2,column]],3,TRUE
>  meandiffs<-unlist(apply(get(samplenames[column]),2,meandiff))
>  cat(samplenames[column],"\n")
>  cat("mean diff =",mean(meandiffs),"95% CI =",
>   quantile(meandiffs,c(0.025,0.975)),"\n")
>  png(paste0(samplenames[column],".png")
>  hist(meandiffs)
>  dev.off()
> }
>
> You should get a printout of the means and CIs and  bunch of PNG files with
> the histograms.
>
> Jim
>
>
> On Tue, Sep 11, 2018 at 11:55 PM Kristi Glover 
> wrote:
>
> > Dear Jim,
> >
> > Thank you very much for the code. I run it but it gave me row names
> > like "year224", "year142".
> >
> > are these the difference between columns? If we want to get bootstrapping
> > means of difference between years (year2-year1; year3-year1), its CI and
> > exact p value, how can we get it?
> >
> > thanks
> >
> > KG
> >
> > 
> >
> > head(daT)
> >
> > colname.mat<-combn(paste0("year",1:4),2)
> >
> > samplenames<-apply(colname.mat,2,paste,collapse="")
> >
> > k<-10
> >
> > for(column in 1:ncol(colname.mat)) {
> >
> >  assign(samplenames[column],replicate(k,sample(unlist(daT[,colname.mat[,
> > column]]),3,TRUE)))
> >
> > }
> >
> > > get(samplenames[1])
> >  [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
> > year224 0.556 0.667 0.571 0.526 0.629 0.696 0.323 0.526 0.256 0.667
> > year142 0.324 0.324 0.706 0.638 0.600 0.294 0.612 0.688 0.432 0.387
> > year237 0.571 0.696 0.629 0.471 0.462 0.471 0.452 0.595 0.333 0.435
> >
> >
> >
> >
> > --
> > *From:* Jim Lemon 
> > *Sent:* September 11, 2018 1:44 AM
> > *To:* Kristi Glover
> > *Cc:* r-help mailing list
> > *Subject:* Re: [R] loop for comparing two or more groups using
> > bootstrapping
> >
> > Hi Kristy,
> > Try this:
> >
> > colname.mat<-combn(paste0("year",1:4),2)
> > samplenames<-apply(colname.mat,2,paste,collapse="")
> > k<-1
> > for(column in 1:ncol(colname.mat)) {
> >
> >
> assign(samplenames[column],replicate(k,sample(unlist(daT[,colname.mat[,column]]),3,TRUE)))
> > }
> >
> > Then use get(samplenames[1]) and so on to access the values.
> >
> > Jim
> > On Tue, Sep 11, 2018 at 4:52 PM Kristi Glover  >
> > wrote:
> > >
> > > Hi R users,
> > >
> > > I was trying to test a null hypothesis of difference between two groups
> > was 0. I have many years data, such as year1, year2,, year3, year4 and I
> > was trying to compare between year1 and year2, year1 and year3, year2 and
> > year3 and so on and have used following code with an example data.
> > >
> > >
> > > I tried to make a loop but did not work to compare between many years,
> > and also want to obtain the exact p value. Would you mind to help me to
> > make a loop?
> > >
> > > Thanks for your help.
> > >
> > >
> > > KG
> > >
> > >
> > > daT<-structure(list(year1 = c(0.417, 0.538, 0.69, 0.688, 0.688, 0.606,
> > > 0.667, 0.7, 0.545, 0.462, 0.711, 0.642, 0.744, 0.604, 0.612,
> > > 0.667, 0.533, 0.556, 0.444, 0.526, 0.323, 0.308, 0.195, 0.333,
> > > 0.323, 0.256, 0.345, 0.205, 0.286, 0.706, 0.7, 

Re: [R] how can I convert a long to wide matrix?

2018-05-01 Thread Marna Wagley
Hi Jim,
The data set is correct. I took two readings from the "SITE A" within a
short time interval, therefore I want to take the first value if there are
repeated within a same group of "timeGroup".
Therefore I wanted following

FinalData1
 B1B2
id_X   "A"   "B"
id_Y   "A"   "B"

thanks,



On Tue, May 1, 2018 at 4:05 PM, Jim Lemon <drjimle...@gmail.com> wrote:

> Hi Marna,
> I think this is due to having three rows for id_X and only two for
> id_Y. The function creates a data frame with enough columns to hold
> the greatest number of values for each ID variable. Notice that the
> SITE_n columns contain three values for id_X (A, A, B) and two for
> id_Y (A, B, NA) as there was no third occasion of measurement for the
> latter. Even though there are only two _values_ for SITE, there must
> be enough space for three. In your desired output, SITE for the second
> occasion of measurement is wrong (it should be "A"), and for the third
> occasion it is unknown. Even if there was only one value for SITE in
> the original data frame, it should be repeated for the correct number
> of observations. I think you may be mixing up case ID with location of
> observation.
>
> Jim
>
>
> On Wed, May 2, 2018 at 8:48 AM, Marna Wagley <marna.wag...@gmail.com>
> wrote:
> > Hi Jim,
> > Thank you very much for your suggestions. I used it but it gave me three
> > sites. But actually I do have only two sites "Id_X" and "Id_y" . In fact
> > "A" is repeated two times for "Id_X". If it is repeated, I would like to
> > take the first one among many repeated values.
> >
> > dat<-structure(list(ID = structure(c(1L, 1L, 1L, 2L, 2L), .Label =
> c("id_X",
> >
> > "id_Y"), class = "factor"), EventDate = structure(c(4L, 5L, 2L,
> >
> > 3L, 1L), .Label = c("9/15/16", "9/15/17", "9/7/16", "9/8/16",
> >
> > "9/9/16"), class = "factor"), timeGroup = structure(c(1L, 1L,
> >
> > 2L, 1L, 2L), .Label = c("B1", "B2"), class = "factor"), SITE =
> > structure(c(1L,
> >
> > 1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor")), .Names =
> c("ID",
> >
> > "EventDate", "timeGroup", "SITE"), class = "data.frame", row.names =
> c(NA,
> >
> > -5L))
> >
> > library(prettyR)
> >
> > stretch_df(dat,idvar="ID",to.stretch=c("EventDate","SITE"))
> >
> >
> > ID timeGroup EventDate_1 EventDate_2 EventDate_3 SITE_1 SITE_2 SITE_3
> > 1 id_XB1  9/8/16  9/9/16 9/15/17  A  A  B
> > 2 id_YB1  9/7/16 9/15/16  A  B   
> >>
> >
> > Basically I am looking for like following table
> >
> > ID timeGroup EventDate_1 EventDate_2 EventDate_3 SITE_1 SITE_2
> > 1 id_XB1  9/8/16  9/9/16 9/15/17  A  B
> > 2 id_YB1  9/7/16 9/15/16  A  B
> >
> > Thanks
> >
> >
> > On Tue, May 1, 2018 at 3:32 PM, Jim Lemon <drjimle...@gmail.com> wrote:
> >>
> >> Hi Marna,
> >> Try this:
> >>
> >> library(prettyR)
> >> stretch_df(dat,idvar="ID",to.stretch=c("EventDate","SITE"))
> >>
> >> Jim
> >>
> >>
> >> On Wed, May 2, 2018 at 8:24 AM, Marna Wagley <marna.wag...@gmail.com>
> >> wrote:
> >> > Hi R user,
> >> > I was trying to convert a long matrix to wide? I have an example and
> >> > would
> >> > like to get a table (FinalData1):
> >> >
> >> >
> >> > FinalData1
> >> >  B1B2
> >> > id_X   "A"   "B"
> >> > id_Y   "A"   "B"
> >> >
> >> > but I got the following table using the following code.
> >> >
> >> > FinalData1
> >> >
> >> >  B1  B2
> >> >
> >> > id_X "A" "A"
> >> >
> >> > id_Y "A" "B"
> >> >
> >> >
> >> > the code and the example data I used are given below. Is there any
> >> > suggestions to fix the problem?
> >> >
> >> >
> >> > dat<-structure(list(ID = structure(c(1L, 1L, 1L, 2L, 2L), .Label =
> >> > c("id_X

Re: [R] how can I convert a long to wide matrix?

2018-05-01 Thread Marna Wagley
Hi Jim,
Thank you very much for your suggestions. I used it but it gave me three
sites. But actually I do have only two sites "Id_X" and "Id_y" . In fact
"A" is repeated two times for "Id_X". If it is repeated, I would like to
take the first one among many repeated values.

dat<-structure(list(ID = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("id_X",


"id_Y"), class = "factor"), EventDate = structure(c(4L, 5L, 2L,

3L, 1L), .Label = c("9/15/16", "9/15/17", "9/7/16", "9/8/16",

"9/9/16"), class = "factor"), timeGroup = structure(c(1L, 1L,

2L, 1L, 2L), .Label = c("B1", "B2"), class = "factor"), SITE = structure(c(
1L,

1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor")), .Names = c("ID",

"EventDate", "timeGroup", "SITE"), class = "data.frame", row.names = c(NA,

-5L))

library(prettyR)

stretch_df(dat,idvar="ID",to.stretch=c("EventDate","SITE"))

ID timeGroup EventDate_1 EventDate_2 EventDate_3 SITE_1 SITE_2 SITE_3
1 id_XB1  9/8/16  9/9/16 9/15/17  A  A  B
2 id_YB1  9/7/16 9/15/16  A  B   
>

Basically I am looking for like following table

ID timeGroup EventDate_1 EventDate_2 EventDate_3 SITE_1 SITE_2
1 id_XB1  9/8/16  9/9/16 9/15/17  A  B
2 id_YB1  9/7/16 9/15/16  A  B

Thanks


On Tue, May 1, 2018 at 3:32 PM, Jim Lemon <drjimle...@gmail.com> wrote:

> Hi Marna,
> Try this:
>
> library(prettyR)
> stretch_df(dat,idvar="ID",to.stretch=c("EventDate","SITE"))
>
> Jim
>
>
> On Wed, May 2, 2018 at 8:24 AM, Marna Wagley <marna.wag...@gmail.com>
> wrote:
> > Hi R user,
> > I was trying to convert a long matrix to wide? I have an example and
> would
> > like to get a table (FinalData1):
> >
> >
> > FinalData1
> >  B1B2
> > id_X   "A"   "B"
> > id_Y   "A"   "B"
> >
> > but I got the following table using the following code.
> >
> > FinalData1
> >
> >  B1  B2
> >
> > id_X "A" "A"
> >
> > id_Y "A" "B"
> >
> >
> > the code and the example data I used are given below. Is there any
> > suggestions to fix the problem?
> >
> >
> > dat<-structure(list(ID = structure(c(1L, 1L, 1L, 2L, 2L), .Label =
> c("id_X",
> >
> >
> > "id_Y"), class = "factor"), EventDate = structure(c(4L, 5L, 2L,
> >
> > 3L, 1L), .Label = c("9/15/16", "9/15/17", "9/7/16", "9/8/16",
> >
> > "9/9/16"), class = "factor"), timeGroup = structure(c(1L, 1L,
> >
> > 2L, 1L, 2L), .Label = c("B1", "B2"), class = "factor"), SITE =
> structure(c(
> > 1L,
> >
> > 1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor")), .Names =
> c("ID",
> >
> > "EventDate", "timeGroup", "SITE"), class = "data.frame", row.names =
> c(NA,
> >
> > -5L))
> >
> >
> > tmp <- split(dat, dat$ID)
> >
> > tmp1 <- do.call(rbind, lapply(tmp, function(dat){
> >
> > tb <- table(dat$timeGroup)
> >
> > idx <- which(tb>0)
> >
> > tb1 <- replace(tb, idx, as.character(dat$SITE))
> >
> > }))
> >
> >
> > tmp1
> >
> > FinalData<-print(tmp1, quote=FALSE)
> >
> > [[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-project.org/
> posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] how can I convert a long to wide matrix?

2018-05-01 Thread Marna Wagley
Hi R user,
I was trying to convert a long matrix to wide? I have an example and would
like to get a table (FinalData1):


FinalData1
 B1B2
id_X   "A"   "B"
id_Y   "A"   "B"

but I got the following table using the following code.

FinalData1

 B1  B2

id_X "A" "A"

id_Y "A" "B"


the code and the example data I used are given below. Is there any
suggestions to fix the problem?


dat<-structure(list(ID = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("id_X",


"id_Y"), class = "factor"), EventDate = structure(c(4L, 5L, 2L,

3L, 1L), .Label = c("9/15/16", "9/15/17", "9/7/16", "9/8/16",

"9/9/16"), class = "factor"), timeGroup = structure(c(1L, 1L,

2L, 1L, 2L), .Label = c("B1", "B2"), class = "factor"), SITE = structure(c(
1L,

1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor")), .Names = c("ID",

"EventDate", "timeGroup", "SITE"), class = "data.frame", row.names = c(NA,

-5L))


tmp <- split(dat, dat$ID)

tmp1 <- do.call(rbind, lapply(tmp, function(dat){

tb <- table(dat$timeGroup)

idx <- which(tb>0)

tb1 <- replace(tb, idx, as.character(dat$SITE))

}))


tmp1

FinalData<-print(tmp1, quote=FALSE)

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to replace numeric value in the column contains Text (Factor)?

2018-04-18 Thread Marna Wagley
Hi David and Jeff,
Both ways worked for my table. it helped me a lot (I was really struggling
on it to change the values and thinking to do manually). You are great.
Thank you

On Wed, Apr 18, 2018 at 11:13 AM, David L Carlson <dcarl...@tamu.edu> wrote:

> The simplest would be to convert precip to character and then back to a
> factor if you really want it to be a factor. This will also remove the
> levels that no longer exist.
>
> str(dat)
> # 'data.frame':   5 obs. of  3 variables:
> #  $ Sites : Factor w/ 5 levels "Site1","Site2",..: 1 2 3 4 5
> #  $ temp  : num  14 15 12 12.5 17
> #  $ precip: Factor w/ 5 levels "15","34","high",..: 3 4 5 2 1
>
> dat$precip <- as.character(dat$precip)
> dat[4:5, 3] <-"20"
> dat$precip <- factor(dat$precip)
>
> str(dat)
> # 'data.frame':   5 obs. of  3 variables:
> #  $ Sites : Factor w/ 5 levels "Site1","Site2",..: 1 2 3 4 5
> #  $ temp  : num  14 15 12 12.5 17
> #  $ precip: Factor w/ 4 levels "20","high","low",..: 2 3 4 1 1
>
> 
> David L Carlson
> Department of Anthropology
> Texas A University
> College Station, TX 77843-4352
>
> -Original Message-
> From: R-help <r-help-boun...@r-project.org> On Behalf Of Marna Wagley
> Sent: Wednesday, April 18, 2018 12:56 PM
> To: r-help mailing list <r-help@r-project.org>
> Subject: [R] How to replace numeric value in the column contains Text
> (Factor)?
>
> Hi R user,
> Would you mind to help me on how I can change a value in a specific column
> and row in a big table? but the column of the table is a factor (not
> numeric).
> Here is an example. I want to change dat[4:5,3]<-"20" but it generated NA>
> do you have any suggestions for me?
>
> dat<-structure(list(Sites = structure(1:5, .Label = c("Site1", "Site2",
> "Site3", "Site4", "Site5"), class = "factor"), temp = c(14, 15, 12, 12.5,
> 17), precip = structure(c(3L, 4L, 5L, 2L, 1L), .Label = c("15", "34",
> "high", "low", "medium"), class = "factor")), .Names = c("Sites", "temp",
> "precip"), class = "data.frame", row.names = c(NA, -5L
> ))
> > dat[4:5, 3] <-"20"
> Warning message:
> In `[<-.factor`(`*tmp*`, iseq, value = c("20", "20")) :
>   invalid factor level, NA generated
> Thanks,
>
> [[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-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to replace numeric value in the column contains Text (Factor)?

2018-04-18 Thread Marna Wagley
Hi R user,
Would you mind to help me on how I can change a value in a specific column
and row in a big table? but the column of the table is a factor (not
numeric).
Here is an example. I want to change dat[4:5,3]<-"20" but it generated NA>
do you have any suggestions for me?

dat<-structure(list(Sites = structure(1:5, .Label = c("Site1", "Site2",
"Site3", "Site4", "Site5"), class = "factor"), temp = c(14, 15,
12, 12.5, 17), precip = structure(c(3L, 4L, 5L, 2L, 1L), .Label = c("15",
"34", "high", "low", "medium"), class = "factor")), .Names = c("Sites",
"temp", "precip"), class = "data.frame", row.names = c(NA, -5L
))
> dat[4:5, 3] <-"20"
Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = c("20", "20")) :
  invalid factor level, NA generated
Thanks,

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] repeating functions for different folders?

2018-04-12 Thread Marna Wagley
Hi R users,
I need to run a analysis using a data for each folder. I do have several
folders. Each folder contains several files but these files name are
similar to the files that is saved into  another folders.  I need to repeat
the analysis for every folder and would like to save the output in that
particular folder.  After completing the analysis in one folder, I want to
move another folder. Basically I was doing manually (repeating the analysis
each and every folder manually). Is there any way to make a loop so that I
can run the analysis for different folders? Is there any example code?

Thanks for your help.
MW

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to create separate legend for each plot in the function of facet_wrap in ggplot2?

2017-11-10 Thread Marna Wagley
Hi R users,
I need to create more than 20 figures (one for each group) in one page. I
have a  common  legend for 20 figures using the facet_wrap. However the
range of the values among the groups are very wide. For example one group
has the value of 0 to 3, but the values of some of the groups has ranged
from 0 to 20 so that when I used a single common legend for all 20 figures,
I could not display the contrast of the values in some of the figures.
Therefore I wanted to create the figures with *a separate legend*.In this
way, I can display the gradient of the values in each figure.  Any
suggestions on how I can create it.

The example is given below, *I wanted to create a separate legend with
keeping legend inside of each of the figure*.

library(ggplot2)

dat<-structure(list(X = c(289.6, 289.7, 289.8, 289.9, 290, 290.1,

927.8, 927.9, 928, 928.1, 928.2, 928.3), Y = c(789.1, 789.2,

789.3, 789.4, 789.5, 789.6, 171.1, 171.2, 171.3, 171.4, 171.5,

171.6), value = c(0.05, 0.06, 0.07, 0.09, 0.1, 0.11, 0.06, 0.05,

0.05, 0.06, 0.1, 1.5), group = structure(c(1L, 1L, 1L, 1L, 1L,

1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor")),
.Names = c("X",

"Y", "value", "group"), class = "data.frame", row.names = c(NA,

-12L))


AB<-ggplot(data = dat, aes(x = X, y = Y, color =  value)) +  geom_point(size
=2) +

coord_equal() +  theme_bw()+ scale_color_gradientn(colours = terrain.colors(
7))

AB+facet_wrap(~group,  scales="free")+theme(strip.text = element_text(size
= 8))




Thanks


MW

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] create a loop

2017-10-20 Thread Marna Wagley
Hi R Users,
I do have very big data sets and wanted to run some of the analyses many
times with randomization (1000 times).
I have done the analysis using an example data but it need to be done with
randomized data (1000 times). I am doing manually for 1 times but
taking so much time, I wonder whether it is possible to perform the
analysis with creating a loop for many replicated datasets?  The code and
the example data sets are attached.

I will be very grateful if someone help me to create the loop for the
following example data and the analyses.

I appreciate  your help.


MW

#

dat1<-structure(list(RegionA = structure(c(1L, 1L, 2L, 3L, 3L, 4L, 5L, 5L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("Ra", "Rb", "Rc",
"Rd", "Re", "Rf"), class = "factor"), site = structure(c(1L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label
= c("s1", "s10", "s11", "s12", "s13", "s14", "s15", "s16", "s17", "s18",
"s19", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9"), class = "factor"),
temp = c(23L, 21L, 10L, 15L, 16L, 8L, 13L, 1L, 23L, 19L, 25L, 19L, 12L, 16L,
19L, 21L, 12L, 5L, 7L), group = structure(c(1L, 1L, 1L, 2L, 2L, 2L,

2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", "B",
"C"), class = "factor")), .Names = c("RegionA", "site", "temp", "group"),
class = "data.frame", row.names = c(NA, -19L))

head(dat1)


dat2<-structure(list(group = structure(1:3, .Label = c("A", "B", "C"

), class = "factor"), totalP = c(250L, 375L, 180L), sampled = c(25L,

37L, 27L)), .Names = c("group", "total.pop", "sampled.pop"), class =
"data.frame", row.names = c(NA,

-3L))


##

idx <- 1:nrow(dat1)

lll <- split(idx, dat1$group)


##

#Replication 1 create a resampled data



Replication1<-dat1[unlist(lapply(lll, sample, rep=TRUE)),]


Summary.Rep1<-ddply(Replication1, c("group"), summarise,

N= length(group),

   mean = mean(temp, na.rm=TRUE),

   sd   = sd(temp),

   se   = sd / sqrt(N),

   variance=sd^2

)

#merge two datasets (dat1 and dat2)

Rep1<-merge(Summary.Rep1, dat2, by="group")


#calclate adjusted mean. variance

Rep1$adj.mean<-(Rep1$total.pop*Rep1$mean)/sum(Rep1$total.pop)

Rep1$adj.var<-(Rep1$variance)/(Rep1$sampled.pop/(1-(Rep1$sampled.pop/Rep1$
total.pop)))

Rep1$over.adj.var<-(Rep1$total.pop/sum(Rep1$total.pop))^2*Rep1$adj.var


Rep1$total<-Rep1$adj.mean*(Rep1$total.pop)

##

Estimated.TotalTemp<-sum(Rep1$adj.mean)*sum(Rep1$total.pop)

Estimated.totalvar<-sum(Rep1$adj.var)

Estimated.SE<-sqrt(Estimated.totalvar)*sum(Rep1$total.pop)

RESULTS.R1<-data.frame(Estimated.TotalTemp, SE=Estimated.SE)

RESULTS.R1




##

#Replication 2 create a resampled data



Replication2<-dat1[unlist(lapply(lll, sample, rep=TRUE)),]


Summary.Rep2<-ddply(Replication2, c("group"), summarise,

N= length(group),

   mean = mean(temp, na.rm=TRUE),

   sd   = sd(temp),

   se   = sd / sqrt(N),

   variance=sd^2

)

#merge two datasets

Rep1<-merge(Summary.Rep2, dat2, by="group")


#calclate adjusted mean. variance

Rep2$adj.mean<-(Rep2$total.pop*Rep2$mean)/sum(Rep2$total.pop)

Rep2$adj.var<-(Rep2$variance)/(Rep2$sampled.pop/(1-(Rep2$sampled.pop/Rep2$
total.pop)))

Rep2$over.adj.var<-(Rep2$total.pop/sum(Rep2$total.pop))^2*Rep2$adj.var


Rep2$total<-Rep2$adj.mean*(Rep2$total.pop)


##

Estimated.TotalTemp<-sum(Rep2$adj.mean)*sum(Rep2$total.pop)

Estimated.totalvar<-sum(Rep2$adj.var)

Estimated.SE<-sqrt(Estimated.totalvar)*sum(Rep2$total.pop)

RESULTS.R2<-data.frame(Estimated.TotalTemp, SE=Estimated.SE)


##

#combined all results from 1000 runs

ALL.Results(Restult.R1, Result.R2)

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] creating tables with replacement

2017-10-18 Thread Marna Wagley
Dear David and Petr,
It worked and I think now I can modify the code little bit to fit my
requirement. Thank you so much for your help.

Thanks,

MW

On Wed, Oct 18, 2017 at 7:15 AM, David L Carlson <dcarl...@tamu.edu> wrote:

> Building on Petr's suggestion, you could modify his code to get all 10
> samples at once in a compact format:
>
> > Samples <- lapply(lll, function(x) replicate(10, sample(x, rep=TRUE)))
> # Samples is a list containing 3 matrices, one for each group
> # Each column gives the index (row) numbers for a particular sample
> > str(Samples)
> List of 3
>  $ A: int [1:3, 1:10] 2 1 3 1 1 3 2 3 1 2 ...
>  $ B: int [1:7, 1:10] 8 5 8 6 5 8 10 6 4 7 ...
>  $ C: int [1:9, 1:10] 14 15 19 13 18 19 19 12 13 16 ...
> # Row numbers for all 10 samples of 3 for group A
> > Samples$A
>  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,]212233232 3
> [2,]113133233 3
> [3,]331221233 1
> # The first sample from group A
> > dat1[Samples$A[ , 1], ]
>   RegionA site temp group
> 2  Ra   s2   21 A
> 1  Ra   s1   23 A
> 3  Rb   s3   10 A
>
> # Save the first sample from group A as a .csv file
> > write.csv(dat1[Samples$A[ , 1], ], row.names=FALSE, file="Test.csv")
>
> # Save all ten group A samples as a single .csv file
> # Faster but you have to split them into separate tables by hand
> > write.csv(dat1[Samples$A[ , 1:10], ], row.names=FALSE, file="Test.csv")
>
> 
> David L Carlson
> Department of Anthropology
> Texas A University
> College Station, TX 77843-4352
>
>
> -----Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of PIKAL Petr
> Sent: Wednesday, October 18, 2017 3:38 AM
> To: Marna Wagley <marna.wag...@gmail.com>; r-help mailing list <
> r-help@r-project.org>
> Subject: Re: [R] creating tables with replacement
>
> Hi
>
> maybe there is another more elegant solution but something like this
>
> > idx <- 1:nrow(dat1)
> > lll <- split(idx, dat1$group)
> > dat1[unlist(lapply(lll, sample, rep=TRUE)),]
>
> gives you selected rows.
>
> You could use for cycle or save those data frames manually
>
> Cheers
>
> Petr
> > -Original Message-
> > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Marna
> > Wagley
> > Sent: Wednesday, October 18, 2017 9:46 AM
> > To: r-help mailing list <r-help@r-project.org>
> > Subject: [R] creating tables with replacement
> >
> > Hi R User,
> > I am new in R and trying to create tables with selecting rows randomly
> > (but with replacement) for each group but each group should have same
> > number as original. Is it possible to create it using the following
> example data set?
> >
> > Your help is highly appreciated.
> >
> > dat1<-structure(list(RegionA = structure(c(1L, 1L, 2L, 3L, 3L, 4L,
> >
> > 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("Ra",
> >
> > "Rb", "Rc", "Rd", "Re", "Rf"), class = "factor"), site =
> > structure(c(1L,
> >
> > 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L,
> >
> > 8L, 9L, 10L, 11L), .Label = c("s1", "s10", "s11", "s12", "s13",
> >
> > "s14", "s15", "s16", "s17", "s18", "s19", "s2", "s3", "s4", "s5",
> >
> > "s6", "s7", "s8", "s9"), class = "factor"), temp = c(23L, 21L,
> >
> > 10L, 15L, 16L, 8L, 13L, 1L, 23L, 19L, 25L, 19L, 12L, 16L, 19L,
> >
> > 21L, 12L, 5L, 7L), group = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
> >
> > 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A",
> >
> > "B", "C"), class = "factor")), .Names = c("RegionA", "site",
> >
> > "temp", "group"), class = "data.frame", row.names = c(NA, -19L
> >
> > ))
> >
> >
> > Here group A has 3 rows,
> >
> > B had 7 rows, and
> >
> > C has 9 rows
> >
> >
> > I want to select rows randomly (with replacement) for each group but
> > each group should have same number of rows as above (group A=3 rows,
> > B=7 rows, and
> > c=9 rows). This way I want to create at least 10 tabl

[R] creating tables with replacement

2017-10-18 Thread Marna Wagley
Hi R User,
I am new in R and trying to create tables with selecting rows randomly (but
with replacement) for each group but each group should have same number as
original. Is it possible to create it using the following example data set?

Your help is highly appreciated.

dat1<-structure(list(RegionA = structure(c(1L, 1L, 2L, 3L, 3L, 4L,

5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("Ra",

"Rb", "Rc", "Rd", "Re", "Rf"), class = "factor"), site = structure(c(1L,

12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L,

8L, 9L, 10L, 11L), .Label = c("s1", "s10", "s11", "s12", "s13",

"s14", "s15", "s16", "s17", "s18", "s19", "s2", "s3", "s4", "s5",

"s6", "s7", "s8", "s9"), class = "factor"), temp = c(23L, 21L,

10L, 15L, 16L, 8L, 13L, 1L, 23L, 19L, 25L, 19L, 12L, 16L, 19L,

21L, 12L, 5L, 7L), group = structure(c(1L, 1L, 1L, 2L, 2L, 2L,

2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A",

"B", "C"), class = "factor")), .Names = c("RegionA", "site",

"temp", "group"), class = "data.frame", row.names = c(NA, -19L

))


Here group A has 3 rows,

B had 7 rows, and

C has 9 rows


I want to select rows randomly (with replacement) for each group but each
group should have same number of rows as above (group A=3 rows, B=7 rows, and
c=9 rows). This way I want to create at least 10 tables (10 random tables)
and save them separately in Excel.


Thanks

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Any help on R code interpretation?

2016-12-22 Thread Marna Wagley
HI R user,
I was looking a r code and saw "%*%t", what does it("%*%t") mean?. The
example is given below.

For example: here is the code where "%*%t" has been used. when I run the
formula but did not run in my data.

prediction=plogis(as.matrix(mod7)%*%t(with(subset(data,site!='AB'),cbind(1,
site=='BC', site =='MM', site =='XY',Temp,Treatment=='Local'

Can I get the same result of above using the following code? Do both codes
give same results?

ab<-with(subset(data,site!='AB'),cbind(1,site=='BC', site =='MM', site ==
'XY',Temp,Treatment=='Local'))
prediction =plogis(as.matrix(mod7),(ab))

Thanks,

MW

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] how to show a plot without overlaying the text on top of the another text?

2016-12-13 Thread Marna Wagley
Hi R user,
I have created using metaNMDS (Nonmetirc Multidimensional Scaling, MDS) to
show species composition but some of the species are concentrated at some
of the sites so that the name of the species are overlaid and it was
almost impossible to read the species name in the figure. I was wondering how
we can show the plot without overlaying the text on top of another.
I used the following to create the plot. would you mind to suggest me?

comm.bc.mds <- metaMDS(dat, dist = "bray")
ordiplot(comm.bc.mds, display = "sites", type = "text")
ordipointlabel(comm.bc.mds,font = c(1, 1), cex = c(0.5, 0.3))

Thanks

MW

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] how to randomly select the samples with different probabilities for different classes?

2016-12-07 Thread Marna Wagley
Hi R user,
I have samples with covariates for different classes, I wanted to choose
the samples of different groups with different probabilities. For example,
I have a 22 samples size with 3 classes,
groupA has 8 samples
groupB has 8 samples
groupC has 6 samples

I want to select a total 14 samples from 22 samples, in which  40% of the
14 samples should be in groups A and B, 60% of the 14 samples should be in
the group C.

Would you mind to help me on how I can select the samples with that
conditions? I have attached a sample data

dat<-structure(list(sampleID = c(17L, 21L, 36L, 45L, 67L, 82L, 90L,
31L, 70L, 45L, 24L, 80L, 82L, 45L, 85L, 14L, 81L, 96L, 61L, 12L,
65L, 88L), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A",
"B", "C"), class = "factor")), .Names = c("sampleID", "group"
), class = "data.frame", row.names = c(NA, -22L))

thanks,
  MW

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] can we visualize water flows with 3d in R?

2016-10-14 Thread Marna Wagley
Thanks for everyone for your suggestions. I am trying to use the techniques
that you suggested and I will update you its outcome so that it might be
useful to other too.
Thanks,


On Thu, Oct 13, 2016 at 2:10 PM, Duncan Murdoch <murdoch.dun...@gmail.com>
wrote:

> On 13/10/2016 11:14 AM, Thomas Adams wrote:
>
>> Duncan,
>>
>> Oh, to be sure, with a fair amount of work, you're probably correct that
>> one could mash up something. Here are some examples:
>>
>> http://www.illinoisfloods.org/documents/2013_IAFSM_Conferenc
>> e/Conference_Presentations/5C-1_HEC-GeoRAS_Part1.pdf
>> <--- lots of graphics
>>
>> http://rivergis.com/
>>
>> also...
>> http://www2.egr.uh.edu/~aleon3/courses/Transient_flows/
>> Tutorials/Geo_RAS/georastutorial.pdf
>> -- pages 35->
>> https://www.crwr.utexas.edu/reports/pdf/1999/rpt99-1.pdf -- pages 70->
>> (figures 4-17, 4-18), p. 147
>>
>
> Thanks.  I guess it's up to Marna to say whether any of those figures are
> like what she wants to produce from her data.
>
> Duncan Murdoch
>
>
>> Best,
>> Tom
>>
>> On Thu, Oct 13, 2016 at 9:20 AM, Duncan Murdoch
>> <murdoch.dun...@gmail.com <mailto:murdoch.dun...@gmail.com>> wrote:
>>
>> On 13/10/2016 8:35 AM, Thomas Adams wrote:
>>
>> All,
>>
>> Very respectfully, there are no R packages that can do what
>> Marna desires.
>>
>>
>> I would guess that's not literally true, in that there are several
>> graphics packages that are very flexible.   You could well be right
>> that there are none that are designed specifically for this purpose,
>> so she's probably going to have to do some work to get what she wants.
>>
>> His/Her data, undoubtably, comes from a 1-D hydraulic model
>> simulation -- where output is generated at channel
>> cross-sections -- representing the sloping water surface
>> elevation of the centerline of flow in a stream or river. With
>> mapping software for such problems, the assumption is made that
>> the water surface intersects the topography (within or beyond
>> the stream channel) perpendicular to the direction of flow.
>> Hydrodynamically, this is generally not correct, but it's a
>> reasonable approximation. To do this, typically, the topography
>> -- in the from of a raster digital elevation model (DEM) -- is
>> converted to a triangular irregular network (TIN) to facilitate
>> the creation of a smoother line of intersection between the
>> water surface and topography. Because, the water surface slopes
>> in a downstream direction, contour lines are crossed. Hydraulic
>> modeling software usually is accompanied by this mapping
>> capability, such as with HEC-RAS with RAS-Mapper, developed by
>> the US Army Corps of Engineers, or with HEC-GeoRAS, which
>> requires ESRI ARC GIS; but, there is also a QGIS plugin module
>> that can do this, I believe. These software packages do
>> facilitate representing the flow in 3D.
>>
>>
>> Do you know any sample figures online that would show the type of
>> graph that is usually used here?
>>
>> Duncan Murdoch
>>
>>
>> Tom
>>
>>
>> On Wed, Oct 12, 2016 at 6:12 PM, David Winsemius
>> <dwinsem...@comcast.net <mailto:dwinsem...@comcast.net>
>> <mailto:dwinsem...@comcast.net <mailto:dwinsem...@comcast.net>>>
>> wrote:
>>
>>
>> > On Oct 12, 2016, at 4:28 AM, Duncan Murdoch
>> <murdoch.dun...@gmail.com <mailto:murdoch.dun...@gmail.com>
>> <mailto:murdoch.dun...@gmail.com
>>
>> <mailto:murdoch.dun...@gmail.com>>> wrote:
>> >
>> > On 12/10/2016 4:49 AM, Marna Wagley wrote:
>> >> Hi R Users,
>> >> Is it possible to visualize river flow in 3D (latitude,
>> longitude with
>> >> respect to depth)?
>> >> The example of my data looks like. Any suggestions?
>> >>
>> >>> dat1
>> >>long lat depth flow
>> >> 1 1015.9 857  1.00 1.50
>> >> 2 1015.9 857  1.25 1.23
>> >> 3 1015.9 857  0.50 2.00
>> >> 4 1015.9 858  0.10 1.

[R] can we visualize water flows with 3d in R?

2016-10-12 Thread Marna Wagley
Hi R Users,
Is it possible to visualize river flow in  3D (latitude, longitude with
respect to depth)?
The example of my data looks like. Any suggestions?

> dat1
long lat depth flow
1 1015.9 857  1.00 1.50
2 1015.9 857  1.25 1.23
3 1015.9 857  0.50 2.00
4 1015.9 858  0.10 1.95
5 1015.9 858  0.20 1.50
6 1025.0 858  0.30 1.20
7 1025.0 858  0.40 0.50
8 1025.0 858  0.35 0.70
9 1025.0 858  0.24 1.20

Thanks for your help.
thanks

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How can I add text in plot and x axis of figures created in ggplot2?

2016-08-31 Thread Marna Wagley
Hi R users,
I have created four figures using ggplot2, but I am having trouble  to add
"r2=XXX, p=XX" value on the upper left in each figure and also unit of X
axis of each figure are different. I was also trying to write following  :
1.  "rainfall (mm/year") on X axix for fig A.
2. "temp (degree Celsius)" on X axis for fig B
3.  "distance (m)" on X axis for fig C
4.  "survival Proba(%) on X axis for fig D

I am wondering how I can create the figures with the above information

Thank you for your help in advance

Sincerely,

Marna

following code and the example I have used.

dat<-structure(list(x = c(0.31, 0.04, 0.1, 0.54, 0.03, 0.86, 0.97,

0.4, 0.62, 0.3, 0.44, 0.51, 0.03, 0.12, 0.79, 0.3, 0.22, 0.66,

0.75, 0.45), y = c(0.38, 0.61, 0.16, 0.06, 0.42, 0.67, 0.85,

0.11, 0.79, 0.21, 0.84, 0.95, 0.3, 0.47, 0.79, 0.2, 0.34, 0.21,

0.62, 0.25), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,

2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("A",

"B", "C", "D"), class = "factor")), .Names = c("x", "y", "group"

), class = "data.frame", row.names = c(NA, -20L))


gp<-ggplot(data=dat, aes(x=x, y=y))

Gp<-gp + geom_point(size=1, col="blue")

Gp<-Gp+ stat_smooth(method="lm", level=0.99, col="black",formula=y~poly(x,1
))+

coord_cartesian(ylim=c(0, 1))+theme_bw()+

theme(axis.text.y = element_text(angle = 90, vjust = 0))+

ylab *(*"My Y"*)+*theme(
axis.text.x = element_text(size=8))

Gp+ facet_wrap(~group,ncol=5, scales="free_x")

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] calculate sampel size?

2016-04-14 Thread Marna Wagley
Hi R user,
Can we calculate sample size when only mean and SE are given?
Let say one example,  I have mean with SE is  0.54+-0.0517 (mean+-SE). Is
there any way to find the samples (sample size n) in that condition in R?

i think this question is not related to R, I hope you won't mind.
Thanks

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] replace text in table R?

2016-03-08 Thread Marna Wagley
Hi R Users,
I have been struggling to replace texts in a table by new text. but it
seems crazy as of I am doing it manually in R. I have very big files and
some of the text has to be replaced by another class based on another file
if the name corresponds. I was able to perform following example but it
should be easier if there is a loop. Any suggestions on making a loop for
this example?

Here is the example how I did. I want to assign class into dat1 based on
dat2 table.

dat1<-structure(list(ID = structure(1:8, .Label = c("X127", "X128",
"X129", "X130", "X131", "X132", "X133", "X134"), class = "factor"),
Name = structure(1:8, .Label = c("Site1", "Site2", "Site3",
"Site4", "Site5", "Site6", "Site7", "Site8"), class = "factor"),
Time1 = structure(c(4L, 2L, 3L, 5L, 1L, 1L, 4L, 5L), .Label = c("0",
"GT", "R", "Tr", "W2"), class = "factor"), Time2 = structure(c(2L,
1L, 4L, 2L, 1L, 3L, 4L, 1L), .Label = c("0", "GT", "MA",
"UA"), class = "factor"), Time3 = structure(c(5L, 1L, 4L,
4L, 2L, 3L, 3L, 1L), .Label = c("0", "GT", "R", "Tr", "Y7"
), class = "factor")), .Names = c("ID", "Name", "Time1",
"Time2", "Time3"), class = "data.frame", row.names = c(NA, -8L
))

dat1

dat2<-structure(list(site = structure(c(4L, 2L, 5L, 3L, 6L, 7L, 8L,
1L), .Label = c("GT", "MA", "R", "Tr", "UA", "W1", "W2", "Y7"
), class = "factor"), To.be.assinged = structure(c(1L, 2L, 3L,
1L, 4L, 4L, 1L, 2L), .Label = c("A", "B", "C", "D"), class = "factor")),
.Names = c("site",
"To.be.assinged"), class = "data.frame", row.names = c(NA, -8L
))
dat2

A2 <- as.data.frame(lapply(dat1,function(x)
if(is.character(x)|is.factor(x)) gsub("Tr","A",x) else x))
A3 <- as.data.frame(lapply(A2,function(x) if(is.character(x)|is.factor(x))
gsub("MA","B",x) else x))
A4 <- as.data.frame(lapply(A3,function(x) if(is.character(x)|is.factor(x))
gsub("UA","C",x) else x))
A5 <- as.data.frame(lapply(A4,function(x) if(is.character(x)|is.factor(x))
gsub("R","A",x) else x))
A6 <- as.data.frame(lapply(A5,function(x) if(is.character(x)|is.factor(x))
gsub("W1","D",x) else x))
A7 <- as.data.frame(lapply(A6,function(x) if(is.character(x)|is.factor(x))
gsub("W2","D",x) else x))
A8 <- as.data.frame(lapply(A7,function(x) if(is.character(x)|is.factor(x))
gsub("Y7","A",x) else x))
A9 <- as.data.frame(lapply(A8,function(x) if(is.character(x)|is.factor(x))
gsub("GT","B",x) else x))
A9

Your help is highly appreciated.
Thanks

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How do we do correlation for big matrices?

2015-12-26 Thread Marna Wagley
Hi R users,
I have a very big two matrices of 12 columns and over 0.5 million columns
(50,4710) and trying to get correlation value between two tables but I
could not compute it because of big files.
Would you give me any suggestion on how I can do the correlations for the
big files?

I used the following codes and the example data.

df1<-structure(list(X = structure(c(1L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 2L, 3L, 4L), .Label = c("env1", "env10", "env11", "env12",
"env2", "env3", "env4", "env5", "env6", "env7", "env8", "env9"
), class = "factor"), site1 = c(0.38, 0.83, 0.53, 0.48, 0.66,
0.09, 0.21, 0.02, 0.76, 0.62, 0.2, 0.47), site2 = c(0.19, 0.14,
0.66, 0.35, 0.18, 0.24, 0.18, 0.2, 0.86, 0.06, 0.51, 0.29), site3 = c(0.95,
0.51, 0.91, 0.48, 0.74, 0.67, 0.34, 0.72, 0.43, 0.49, 0.1, 0.48
), site4 = c(0.89, 0.54, 0.93, 0.18, 0.99, 0.21, 0.69, 0.29,
0.89, 0.84, 0.45, 0.2), site5 = c(0.38, 0.37, 0.01, 0.26, 0.97,
0.49, 0.39, 0.31, 0.14, 0.83, 0.99, 0.2), site6 = c(0.68, 0.67,
0.6, 0.92, 0.01, 0.04, 0.49, 0.38, 0.5, 0.37, 0.51, 0.17), site7 = c(0.08,
0.54, 0.31, 0.3, 0.77, 0.39, 0.03, 0.51, 0.28, 0.32, 0.86, 0.95
), site8 = c(0.54, 0.26, 0.87, 0.91, 0.12, 0.51, 0.31, 0.67,
0.69, 0.79, 0.76, 0.08), site9 = c(0.1, 0.68, 0.17, 0.44, 0.78,
0.9, 0.16, 0.31, 0.13, 0.34, 0.9, 0.16), site10 = c(0.53, 0.31,
0.88, 0.61, 0.92, 0.44, 0.92, 0.94, 0.55, 0.8, 0.27, 0.07)), .Names =
c("X",
"site1", "site2", "site3", "site4", "site5", "site6", "site7",
"site8", "site9", "site10"), class = "data.frame", row.names = c(NA,
-12L))
df1<-df1[-1]

df2<-structure(list(X = structure(c(1L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 2L, 3L, 4L), .Label = c("env1", "env10", "env11", "env12",
"env2", "env3", "env4", "env5", "env6", "env7", "env8", "env9"
), class = "factor"), site1 = c(0.36, 0.29, 0.09, 0.07, 0.82,
0.88, 0.59, 0.57, 0.2, 0.29, 0.76, 0.2), site2 = c(0.91, 0.87,
0.91, 0.54, 0.53, 0.2, 0.23, 0.16, 0.42, 0.44, 0.01, 0.29), site3 = c(0.96,
0.56, 0.34, 0.34, 0.6, 0.63, 0.28, 0.25, 0.73, 0.45, 0.88, 0.39
), site4 = c(0.73, 0.79, 0.39, 0.59, 0.63, 0.24, 0.69, 0.94,
0.07, 0.23, 0.01, 0.99), site5 = c(0.88, 0.18, 0.37, 0.24, 0.61,
0.61, 0.54, 0.71, 0.12, 0.82, 0.26, 0.5), site6 = c(0.43, 0.52,
0.01, 0.76, 0.41, 0.57, 0.08, 0.75, 0.82, 0.98, 0.61, 0.74),
site7 = c(0.84, 0.14, 0.96, 0.04, 0.41, 0.84, 0.26, 0.59,
0.29, 0.3, 0.76, 0.05), site8 = c(0.12, 0.18, 0.75, 0.23,
0.96, 0.64, 0.33, 0.61, 0.25, 0.13, 0.99, 0.6), site9 = c(0.26,
0.58, 0.32, 0.67, 0.11, 0.8, 0.87, 0.05, 0.03, 0.47, 0.95,
0.81), site10 = c(0.94, 0.63, 0.64, 0.5, 0.94, 0.75, 0.44,
0.57, 0.19, 0.23, 0.08, 0.18)), .Names = c("X", "site1",
"site2", "site3", "site4", "site5", "site6", "site7", "site8",
"site9", "site10"), class = "data.frame", row.names = c(NA, -12L
))
df2<-df2[-1]
df2
# here I put only 12 columns, but as I mentioned above I have more than 1/2
million columns
cor_site<-data.matrix(diag(cor(df1,df2)))
It works fine for a small data but this big files did not work.

Thanks for your suggestions.
MW

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] to change the size of the line in the plot created in ggplot2

2015-12-24 Thread Marna Wagley
Hi Giorgio,
Thank you very much for the code and the link. I read it and also used but
this code changed the line into "dashed" for all variables. As I mentioned
earlier, I wanted to change for only one variable among 7 variables (for
only one variable).
Thanks

MW

On Thu, Dec 24, 2015 at 4:10 AM, Giorgio Garziano <
giorgio.garzi...@ericsson.com> wrote:

> You can do it this way, for example:
>
> geom_line(linetype="dashed", size=1, colour="blue")
>
> Further info at:
>
> http://docs.ggplot2.org/current/geom_line.html
>
>
> --
> GG
>
>
>
> [[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-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] to change the size of the line in the plot created in ggplot2

2015-12-24 Thread Marna Wagley
Hi R user,
I was trying to make a figure for each of four sites. Each site has 7
classes, among the 7 classes, one is average. I am wondering how I can
change the color and size of the line that was average value. I want to
highlight average value.

Similarly, I have been using smooth function therefore I want to highlight
the smoothed line but still I want to show a original data with gray
colour. I tried it different ways but I could not change the line style (or
size) and colour. would you please give me a suggestion on how I can solve
the problem?

I used the following code.

> unique(dat$site)
[1] SiteA SiteC SiteB SiteD
Levels: SiteA SiteB SiteC SiteD

> unique(dat$variable)
[1] D   C   E   Average F   A   B
Levels: A Average B C D E F
A<-ggplot(data=dat, aes(x=Year, y=value, group = variable, colour =
variable)) +geom_line()+theme_bw()+stat_smooth() A+facet_wrap(~ site,
ncol=2)Thanks

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Would you please help me to create a table in R?

2015-12-18 Thread Marna Wagley
Dear Jim,
I am sorry for not explaining the question in a clear way. I am trying to
explain it, let's see how much clear I can make it.

For the given example, (raw.data). Let's say, the 11 birds were marked and
released at site A in a landscape (a combination of sites siteA, siteB,
site C) in 2010.

In 2011, we revisited at the sites (siteA, SiteB, SiteC), among the 11
birds, we recaptured 3 individuals at Site A, 3 at Site B, 1 at site C but
we could not see 4 individuals at any sites.

Again in 2012, we revisited at the sites (SiteA, SiteB, SiteC), we again
recaptured 2 individuals which was recaptured at Site A in 2011 but 1
individual could not seen at any sites (in another words: in 2011 at the
site A  we had recaptured 3 individuals, among them 2 were again seen at
site A, but we could not see one individual in any sites).

Similarly,we had recaptured 3 individuals in 2011 at the site B, but in
2012, among three individuals, one was again recaptured in Site B but 2
individuals could not be seen at any sites.

Likewise, for SiteC in 2012, we had recaptured 1 at that site (siteC), but
in next year (2012), we could not see at any of the sites.

Again, we had not seen 4 individuals in 2011, but later in 2012, among
these 4 individuals, we recaptured one individual for each site  B and C,
and 2 individuals could not be seen.

I have given the example dataset. If it is really confusing and takes your
lots of time, please forget it, I will try to do in Excel manually,but it
is taking so much time and easily making errors. Your help will be very
useful.

Sincerely,


raw.data<-structure(list(Time1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "SiteA", class = "factor"), Time2 =
structure(c(1L, 4L, 2L, 1L, 2L, 1L, 2L, 1L, 3L, 3L, 3L), .Label = c("0",
"SiteA",
"SiteB", "SiteC"), class = "factor"), Time3 = structure(c(2L,
2L, 2L, 1L, 2L, 1L, 1L, 3L, 3L, 1L, 3L), .Label = c("0", "SiteA",
"SiteB"), class = "factor")), .Names = c("Time1", "Time2", "Time3"
), class = "data.frame", row.names = c(NA, -11L))

raw.data

#
table.format<-structure(list(Time1 = structure(c(NA, NA, NA, NA, NA, 1L,
NA,NA, NA, NA, NA, NA, NA, NA, NA, NA), .Label = "Released A", class =
"factor"),Time2 = structure(c(NA, NA, 2L, NA, NA, NA, 3L, NA, NA, NA,
4L, NA, NA, NA, 1L, NA), .Label = c("NotSeen", "Re-captured.at.A",
"Re-captured.at.B", "Re-captured.at.C"), class = "factor"),
Time3 = structure(c(2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L), .Label = c("NotSeen", "Re-captured.at.A",
"Re-captured.at.B", "Re-captured.at.C"), class = "factor")), .Names =
c("Time1","Time2", "Time3"), class = "data.frame", row.names = c(NA, -16L
))

table.format

###
output<-structure(list(Time1 = c(NA, NA, NA, NA, NA, 11L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), Time2 = c(NA, NA, 3L, NA, NA, NA,
3L, NA, NA, NA, 1L, NA, NA, NA, 4L, NA), Time3 = c(2L, NA, NA,
1L, NA, 2L, NA, 1L, 1L, NA, NA, NA, 1L, 1L, NA, 2L)), .Names = c("Time1",
"Time2", "Time3"), class = "data.frame", row.names = c(NA, -16L
))

output

##



On Thu, Dec 17, 2015 at 11:38 PM, Jim Lemon <drjimle...@gmail.com> wrote:

> Hi Marna,
> A bit hard to understand. If raw.data is a record of 11 individuals
> released at site A at Time 1 and recaptured at either A or B or neither at
> Time2 or Time3, it doesn't seem to bear any consistent relationship to the
> numeric coding in table.format or the output at the bottom. Could you
> explain what the correspondence between the tables is?
>
> Jim
>
>
> On Fri, Dec 18, 2015 at 2:33 PM, Marna Wagley <marna.wag...@gmail.com>
> wrote:
>
>> Hi R users,
>> I am struggling to create a table in R. I did in Excel but I have a lots
>> of
>> data and thought it might be easy in  R  but  I am new in R. How to get
>> "output table" for this example data?
>>
>> This is an example.
>>
>> #
>> raw.data<-structure(list(Time1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
>> 1L, 1L, 1L, 1L), .Label = "SiteA", class = "factor"), Time2 =
>> structure(c(1L,
>> 4L, 2L, 1L, 2L, 1L, 2L, 1L, 3L, 3L, 3L), .Label = c("0", "SiteA",
>> "SiteB", "SiteC"), class = "factor"), Time3 = structure(c(2L,
>> 2L, 2L, 1L, 2L, 1L, 1L, 3L, 3L, 1L, 3L), .Label = c("0", "SiteA",
>> "SiteB"), class = "factor"

[R] Would you please help me to create a table in R?

2015-12-17 Thread Marna Wagley
Hi R users,
I am struggling to create a table in R. I did in Excel but I have a lots of
data and thought it might be easy in  R  but  I am new in R. How to get
"output table" for this example data?

This is an example.

#
raw.data<-structure(list(Time1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "SiteA", class = "factor"), Time2 =
structure(c(1L,
4L, 2L, 1L, 2L, 1L, 2L, 1L, 3L, 3L, 3L), .Label = c("0", "SiteA",
"SiteB", "SiteC"), class = "factor"), Time3 = structure(c(2L,
2L, 2L, 1L, 2L, 1L, 1L, 3L, 3L, 1L, 3L), .Label = c("0", "SiteA",
"SiteB"), class = "factor")), .Names = c("Time1", "Time2", "Time3"
), class = "data.frame", row.names = c(NA, -11L))

raw.data

#
table.format<-structure(list(Time1 = structure(c(NA, NA, NA, NA, NA, 1L,
NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), .Label = "Released A", class =
"factor"),
Time2 = structure(c(NA, NA, 2L, NA, NA, NA, 3L, NA, NA, NA,
4L, NA, NA, NA, 1L, NA), .Label = c("Dead", "Re-captured.at.A",
"Re-captured.at.B", "Re-captured.at.C"), class = "factor"),
Time3 = structure(c(2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L), .Label = c("Dead", "Re-captured.at.A",
"Re-captured.at.B", "Re-captured.at.C"), class = "factor")), .Names =
c("Time1",
"Time2", "Time3"), class = "data.frame", row.names = c(NA, -16L
))

table.format

###
output<-structure(list(Time1 = c(NA, NA, NA, NA, NA, 11L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), Time2 = c(NA, NA, 3L, NA, NA, NA,
3L, NA, NA, NA, 1L, NA, NA, NA, 4L, NA), Time3 = c(2L, NA, NA,
1L, NA, 2L, NA, 1L, 1L, NA, NA, NA, 1L, 1L, NA, 2L)), .Names = c("Time1",
"Time2", "Time3"), class = "data.frame", row.names = c(NA, -16L
))

output

I want to get the table like "output". Any possibility to get it in R?

I will really appreciate for your help. I am struggling to create  this
type of table.


Sincerely,
Marna

[[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-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.