Question: I need to print input from the user in reverse order using a function. As a condition, just words (no floats/ int) should be allowed & at least **four** words need to be entered.
e.g.:
How can I help you
--> you help I can How

It should'nt be possible to input: "4 5 6 7" or "There are 2 dogs"
Here's my current code, however I didn't integrate that only strings are allowed so far:

```
def phrase():
    while True:
        user_input = input("Please insert a phrase: ")
        words = user_input.split(" ")
        n = len(words)
        if n >= 4:
            words = words[-1::-1]
            phrase_reverse = " ".join(words)
            print(phrase_reverse)
        else:
            print("Please only insert words and at least 4 ")
            continue
        break

phrase()

```

I tried with `if n<4 and words == str:`, `if n<4 and words != string` etc.. However, that didn't work. Could you please help me solving this issue? Maybe my code is wrong in general.

Answer: 
You will get an error with what you are trying to do. Once you `group_by(animals)`, there are only three columns that `mutate` can operate on. So you could use:

```
cat %>% 
  group_by(animals) %>%
  mutate(across(2:3, cumsum))
# A tibble: 6 x 4
# Groups:   animals [2]
  animals period     weight1 weight2
  <chr>   <date>       <dbl>   <dbl>
1 E1      2021-03-04     704       0
2 E1      2021-03-11    1438     235
3 E1      2021-03-18    2091     560
4 E2      2021-03-04     851       0
5 E2      2021-03-11    1762     148
6 E2      2021-03-18    2591     348

```

But this approach requires you to know what the new indices would be. Better to try something programmatically. If all the columns are weights, you could use:

```
cat %>% 
  group_by(animals) %>%
  mutate(across(starts_with("weight"), cumsum))

```

Or if you just want to operate on all numeric columns:

```
cat %>% 
  group_by(animals) %>%
  mutate(across(where(is.numeric), cumsum))

```

Both of the latter two approaches give your desired output.
Base R solution:

```
num_col_idx <- vapply(df, is.numeric, logical(1))

cbind(df[,!num_col_idx],
      data.frame(do.call(rbind, lapply(
        split(df[, num_col_idx], df$animals), cumsum)), row.names = NULL))

```