jim holtman
2021-Dec-23 06:57 UTC
[R] able to estimate in the excel but not in R, any suggestion?
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)sd mcd 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 + ) + )> > daTsd mcd 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 <marna.wagley at gmail.com> 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Marna Wagley
2021-Dec-23 07:12 UTC
[R] able to estimate in the excel but not in R, any suggestion?
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 <jholtman at gmail.com> 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) > sd mcd 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 > sd mcd 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 <marna.wagley at gmail.com> > 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]