Rowwise - dsofe/coderr GitHub Wiki

Using rowwise() with starts_with() and ends_with()


https://www.tidyverse.org/blog/2020/04/dplyr-1-0-0-rowwise/


# rowwise()
  # need to use this because some calculations cannot be done across rows, like mean--which is done column-wise
# starts_with 
# ends_with 

# trial 1
df1 <- starwars %>%
  mutate(mass_1 = mass,
         mass_2 = mass,
         mass_3 = mass) %>%
  mutate(a_homeworld = homeworld,
         b_homeworld = homeworld,
         c_homeworld = homeworld) %>%
  relocate(mass, .before = "mass_1") %>%
  relocate(homeworld, .before = "a_homeworld")


# NOT RUN -----
#
# EXAMPLE
#
# rowwise() %>%
#   mutate(
#     sum = sum(c_across(w:z))
######
# rowwise() %>%
#   mutate(
#     sum = sum(c_across(cols = starts_with()))


# ANALYZE -----

df2 <- df1 %>%
  rowwise() %>%
  mutate(sum_mass = sum(c_across(cols = starts_with("mass"))))
# GOOD

df3 <- df1 %>%
  rowwise() %>%
  mutate(sum_mass = sum(c_across(starts_with("mass"))))  # excluded `cols =`

df4 <- df3 %>%
  rowwise() %>%
# mutate(count_homeworld = sum
## How to count values, 0/1, across rows?
⚠️ **GitHub.com Fallback** ⚠️