Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pivot_longer() runs the same code for column selection twice #830

Open
mikemahoney218 opened this issue Jan 13, 2023 · 0 comments
Open

pivot_longer() runs the same code for column selection twice #830

mikemahoney218 opened this issue Jan 13, 2023 · 0 comments

Comments

@mikemahoney218
Copy link
Member

Hi folks!

I believe the pivot_longer() section here should be using : to select columns, rather than -plot_id:

In pivoting longer, we also need to specify what columns to reshape. If the columns are directly adjacent as they are here, we don't even need to list the all out: we can just use the `:` operator!
```{r, purl=FALSE}
surveys_long <- surveys_wide %>%
pivot_longer(names_to = "genus", values_to = "mean_weight", cols = -plot_id)
str(surveys_long)
```

Later in the lesson, the text says "we could have also used -", which makes me think this is an accident of copy and paste and the former should have used ::

We could also have used a specification for what columns to exclude. In this example,
we will use all columns _except_ `plot_id` for the names variable. By using the minus sign in the `cols` argument,
we omit `plot_id` from being reshaped
```{r, purl=FALSE}
surveys_wide %>%
pivot_longer(names_to = "genus", values_to = "mean_weight", cols = -plot_id) %>%
head()
```

Happy to contribute a PR if this is unintentional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant