Computing the age in groovy - more realistic way [closed] - datetime

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wrote a piece of code that generates the age more realistic to real life. I assume that we increment our age every year right when we meet the day and month of our birthday, so technically some years are longer and some are shorter, but to us people it doesn't matter. Here is the solution I came up with, I basically wanted to share my solution here, and I also appreciate if someone has a better approach to share.
def today= new GregorianCalendar()
def dob= new GregorianCalendar()
dob.set(Calendar.ERA, GregorianCalendar.AD )
dob.set(Calendar.YEAR, 1983 )
dob.set(Calendar.MONTH, Calendar.MAY )
dob.set(Calendar.DATE, 23)
userMonth=dob.get(Calendar.MONTH)
userDay=dob.get(Calendar.DATE)
todayMonth=today.get(Calendar.MONTH)
todayDay=today.get(Calendar.DATE)
if(todayMonth < userMonth && todayDay < userDay){
println today.get(Calendar.YEAR)-dob.get(Calendar.YEAR)-1
}else{
println today.get(Calendar.YEAR)-dob.get(Calendar.YEAR)
}

This is an open-ended question, so you might want to consider turning it into something less subjective.
That being said, my take would be to use joda-time, in which case you could do something like this:
import org.joda.time.DateTime
import org.joda.time.Years
def yob = new DateTime(1983, 12, 10, 0, 0).toDateMidnight()
def now = DateTime.now().toDateMidnight()
def years = Years.yearsBetween(yob, now).getYears()
println "Years: $years"
If you just want to share your code for someone else to use (which, by the way, is awesome), perhaps you want to create a gist.
[EDIT] I just created one, here
Good luck!

Related

How could I make a function thar goes trough a vector and recognizes the signs of the numbers on it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I was wondering if someone can help me with this, I need a function that goes trough a vector and recognizes the signs of the numbers inside, thats de basic idea, then I'll try to make some modifications so the function counts every number and shows something like "There are 2 positive numbers and 3 negatives"
What about the following approach (I added also functionality for zeros, remove this feature if it is not needed, or merge it with one of the other two conditions):
sign_function <- function(x){
count_pos_numbers = sum(x>0)
count_neg_numbers = sum(x<0)
count_zeros = sum(x==0)
sprintf("There are %d positive numbers, %d negative numbers, and %d zeros.", count_pos_numbers, count_neg_numbers , count_zeros)
}
I hope that I understood your question correctly.

Why does this function include 0 in the list - lists in R [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
As part of a bigger function in R I am creating a list that shall always contain (1:n-1). I noticed that something with this part of my function was wrong and then saw that my list always contained 0, even though I intended it to start at 1. Why does my list contain 0?
N = 10
n = 2
while(n <= N){
existingV = c(1:n-1)
print(existingV)
n = n+1
}
my expected result was:
1
12
123
1234
....
but it actually returns
01
012
0123
01234
...why?
Besides the needed brackets around (n-1), you should think of other ways doing that. Loops in R are very often slow, but R has a nifty feature called vectorization, which may help to speed up your code considerably.
Despite also being a (hidden) loop, the *apply family is worth studying. purrr::map family is another option.
Finally to avoid this rather common trap with 1:n - 1, you could fall back to seq where you can specify start end end in a more verbose way.
Having said that your code could look like this:
purrr::walk(seq(2, 10, 1), ~ print(seq_len(.x - 1)))

if statement without an else or with an else? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am having some trouble interpreting a conditional here. Which one of these approaches would be better if I wanted this condition evaluated? Some code was shared with me that used this approach:
## Option 1
k1=2
a = "k1 is less than or equal 1"
if(k1 > 1) {
a = "k1 is greater than 1"
}
print(a)
But my instinct would be do something like this:
## Option 2
if(k1 > 1) {
a = "k1 is greater than 1"
} else {a = "k1 is less than or equal 1"}
print(a)
Is there any danger to using Option 1? Basically what I am asking here is what is the best practice and is either option more rigorous?
There isn't any danger to using option 1, as it works just as fine as option 2. The else statement just gives the ability to run a piece of code only if k1 is less than or equal to 1.
In a few cases, option 2 isn't so necessary especially if you only care about the condition being true. For example,
if(today's date == birthday){
System.out.println("Happy Birthday!");
} // no need for an else statement unless you want print out "today is not your b-day"
There is not much danger in using option1 , option2 is better as it allows other people to understand the code faster/easier since it is more visible to read.Also, option1 sometimes give error on complicated codes.
But both options are fine in this case.

for loop with dimension of a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a code,and i am going to do a for loop on two gamma distributions.
Given the list of shape parameter, and i name them "d" then i put in d[1] and d[2] in the random gamma function.
I have simplified what I wish to ask here. when i code d[1] in R, output will be the first vector,while when i code d[2] in R, output will be the second vector.
I have a bit lose then how will it iterate if i using for loop for d ?
*
List_1 <- list(c(4,16),c(16/9,4),c(1,16/9),c(.64,1),c(4/9,.64))
for ( d in List_1) ##first parameter is for gamma.1, second is for gamma.2
{
x<-rgamma(25,d[1],1)
y<-rgamma(25,d[2],1)
t<-t.test(x,y)$p.value
}*
I am sorry if i do ask a silly question. Thanks in advance.
In R it is better to avoid for loops due to their poor performance. Since you are starting with a list lapply is a good start:
lapply(List_1, FUN=function(x){t.test(rgamma(25,x[1],1), rgamma(25,x[2],1))$p.value})
The apply function takes your list and then uses the gamma function on the 2 parameters within the t.test. The result will be a list of the five p values, one for each pair
Your code runs fine. I am actually not sure what you are asking here. You can just use print to find the iteratives, if that is what you want, like:
for (d in List_1){
print(d[1])
print(d[2])
}

Remove first row conditionally [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is a very simple problem and I'll probably catch flame for asking this, but I've looked everywhere and I can't find the answer... Or a different approach to my code.
I need to remove the first row of a data frame if the value of the first row and third column are equal to one.
This is what I have so far:
if (foo[1, 3] == 1) {
foo <- foo[-1, ]
}
Is there a different way to do this using only bracket subsetting (avoiding using an if statement)?
Edit:
Edited for clarity.
The code you wrote doesn't remove the first row permanently, it only prints it out. Change that with foo<-foo[-1, ]
Additionally, the code within the if-statement brackets is only one line anyway, you don't technically need them, but some like them for clarity purposes
if (foo[1, 3] == 1) foo <- foo[-1, ]

Resources