Questions tagged [across]

across is a function for use within the Tidyverse set of R packages which is intended to simplify the process of applying functions across columns of a data.frame. Use this tag to ask questions which focus on applying one or more functions to multiple columns with the across function or the rowwise version c_across.

240 questions
2
votes
2 answers

Multiple if_all() in dplyr::filter not working

I have this dataframe: df <- data.frame("dim" = c(1,1,1,1), "pub" = c(0,0,1,1), "sco" = c(0,0,0,0), "wos" = c(1,1,1,0)) I want to filter it by dynamically choosing which of the columns should have 1 or 0 as their value. Example: yes <- c("dim",…
anpami
  • 760
  • 5
  • 17
2
votes
4 answers

Alternative (faster) approach to dynamically create row means for multiple groups of columns

I'm trying to automatically calculate the mean score per row for multiple groups of columns. E.g. a set of columns could represent items of different scales. The columns are also systematically named (scale_itemnumber). For example, the dummy data…
Rasul89
  • 588
  • 2
  • 5
  • 14
2
votes
2 answers

How to use dplyr's coalesce function with group_by() to create one row per person with all values filled in?

I am trying to use coalesce() to produce one row per participant that has their name and their score. Participants had 3 opportunities to fill in their data, and most only came in once (and those that came in multiple times always put in the same…
J.Sabree
  • 2,280
  • 19
  • 48
2
votes
2 answers

Can I mutate many columns according to many other columns with mutate() and across()?

I have a dataset where loads of columns have NA characters because they depend on another column response. For example : df <- data.frame(X_home = c("Yes", "Yes", "No"), X_school = c("No", "Yes", "No"), …
jrdavalos
  • 75
  • 4
2
votes
1 answer

How to add a column name inside a case_when when using mutate_all and across?

I have the following dataframe and I want to mutate all the columns in ENV dataframe to substitute some value with case_when function: metrics <- structure(list(var = c("SST", "SSS"), min = c(-1.82623172259469, 0.0106425614590989), max =…
yuliaUU
  • 1,581
  • 2
  • 12
  • 33
2
votes
4 answers

Using across function in dplyr to a subset of variables

I have a data frame like this: require(dplyr) x_1=rnorm(10,0,1) x_2=rnorm(10,0,1) x_3=rnorm(10,0,1) y_1=rnorm(10,0,1) y_2=rnorm(10,0,1) data=data.frame(cbind(x_1,x_2,x_3,y_1,y_2)) data[1,1]=NA data[2,1]=NA data[5,2]=NA > data x_1 …
student_R123
  • 962
  • 11
  • 30
2
votes
1 answer

R summarize across with multiple functions

I have a data frame where I am grouping by county, and then trying to summarize teh rest of the data using summarise across. Some of the variables I would like to sum across, while other variables I would like to average across Here is my sample…
KLenny
  • 85
  • 6
2
votes
3 answers

How can I relocate mutated columns next to the original colums?

I have made a function which mutates across columns and creates new named columns from each of them. The new colums are put to the right side of the dataframe whereas I would like to have them adjacent to each of the original columns. I am looking…
2
votes
1 answer

Dplyr: using summarise across to take mean of columns only if row value > 0

I have a dataframe of gene expression scores (cells x genes). I also have the cluster that each cell belongs to in stored as a column. I want to calculate the mean expression values per cluster for a group of genes (columns), however, I only want to…
Darren
  • 277
  • 4
  • 17
2
votes
2 answers

How to find the mean of multiple columns based on a second dataset?

Problem I need to use a dictionary dataset to determine which columns from a different dataset I should calculate the mean. Data I will illustrate my case with the iris dataset (a dataset already in R). I have two datasets: The actual data - like…
Ruam Pimentel
  • 1,288
  • 4
  • 16
2
votes
2 answers

how to count non zero values in each category

this is my data df <- structure(list(team_3_F = c("team ", "team ", "site", "site", "team ", "team ", "newyorkish", "newyorkish", "team ", "team ", "newyorkish", "newyorkish", "browingal ", "browingal ", "site", "site", "browingal ", "browingal…
nik
  • 2,500
  • 5
  • 21
  • 48
2
votes
1 answer

How to dynamically pass columns in tidyverse's across

Assuming the following data: df <- data.frame(id = 1:3, result1 = 4:6, result2 = letters[7:9], result1_label = "test label 1", result2_label = "test label 2") I now want to…
deschen
  • 10,012
  • 3
  • 27
  • 50
2
votes
2 answers

Syntax for multiple successive operations across columns in `dplyr`

I'm struggling with the right syntax for multiple successive operations across columns in dplyr. In this data: df <- structure(list(A1 = c(838.611, 824.048, 668.901, 225.075, 0, 0, 341.291, 0, 101.652, 127.341, 0, 297.092, 0,…
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
2
votes
3 answers

Conditionally take value from column1 if the column1 name == first(value) from column2 BY GROUP

I have this fake dataframe: df <- structure(list(Group = c(1L, 1L, 2L, 2L), A = 1:4, B = 5:8, C = 9:12, X = c("A", "A", "B", "B")), class = "data.frame", row.names = c(NA, -4L)) Group A B C X 1 1 1 5 9 A 2 1 2 6 10 A 3 2 3 7 11 B 4…
TarJae
  • 72,363
  • 6
  • 19
  • 66
2
votes
2 answers

Preserving column types when applying ifelse() across columns of different types in R

This seems to be a fairly simple task, but I couldn't figure it out after studying the documentation of ifelse(), dplyr::if_else() and several similar posts on SO about applying ifelse() to multiple columns in a data frame. My goal: I have the…
elarry
  • 521
  • 2
  • 7
  • 20