Questions tagged [dplyr]
Use this tag for questions relating to functions from the dplyr package, such as group_by, summarize, filter, and select.
30,563
questions
0
votes
1
answer
8
views
Troubleshooting adding a row to a table
I'm trying to make a table which looks at the percent response for different answers to survey questions. Answers are 1 (worst) to 5 (best), and I have succeeded in making the resulting table: Table 1
...
1
vote
5
answers
30
views
how to order columns by names containing special symbols R
I have dataframe below:
df <- data.frame(aa = rep(1,4),
ae = rep(2,4),
dd = rep(3,4),
`aa%` = rep(11,4),
`ae%` = rep(22,4),
...
0
votes
2
answers
24
views
Dataframe Structure Problems and Calculation of new Dataframe
So I got this dataframe (dataframe 1):
# A tibble: 6 x 27
# Groups: authority_dic, Full.Name [6]
authority_dic Full.Name Entity `4_2020` `3_2020` `7_2020` `12_2019` `8_2020` `5_2019` `1_2019` `...
1
vote
2
answers
25
views
bind_rows of multiple tibbles in 2 named lists using imap: how to subset properly
This post seems relevant/similar but not quite the same issue: `dplyr::bind_rows` not working while combining listed tibbles
I'm trying to bind the rows of multiple tibbles in 2 named lists. I've been ...
1
vote
3
answers
31
views
In R, replace values across time series based on another column
Actually this is linked to my previous question: Replace values across time series columns based on another column
However I need to modify values across a time series data set but based on a ...
0
votes
2
answers
29
views
Color ggplot by the label
For the following plot, I want to color the points according to the label, whereby all labels that contain "1" are red in their string, labels that contain "2" in their string are ...
1
vote
2
answers
36
views
Replace values across time series columns based on another column
My question is similar to another one: R replace specific value in many columns across dataframe
However I need to replace values based on a vector from another column rather than with NA or a ...
2
votes
2
answers
36
views
Row_wise and column_wise fill NA's using fill and down/across
I have the following data and I am trying to "clean" it. I want to do 3 things:
Fill the NA's in column ...2 to have something like:
Andalucía Almería
Andalucía Cadiz
...
0
votes
2
answers
28
views
Assign NA to a variable based on Group and ifelse condition
I'm trying to assign NA value in column RAIN_15, if any row in RAIN_15 has value > 100 then all observations corresponding to that gr in RAIN_15 will be replaced by NA. Sample data is as given ...
0
votes
3
answers
37
views
R: Round a data frame with a specific formula across all columns with dplyr
I would like to use the following formula to round my whole data frame in R:
format(round(Df$`p-value`, digits=2), nsmall = 2)
Now, the code above is only for one column (just to show you).
I know ...
0
votes
2
answers
33
views
R dplyr summarise mean and stdev using group_by
I have a dataframe that looks like this:
df <- data.frame("Experiment" = c(rep("Exp1", 6), rep("Exp2", 5), rep("Exp3", 4)),
"Replicate&...
1
vote
2
answers
39
views
In R is there a way to create a new column based on column names and values? Tidy solution welcome
Specifically I have an untidy data.frame with subspecies varieties in separate columns, like this;
# Data
Genus<- c("Metrosideros", "Gahnia", "Acacia")
Species<- c(&...
0
votes
1
answer
38
views
Translate SQL statement in R code using dplyr
I need help transalting an SQL statement regarding this dataset https://www.kaggle.com/datasets/hugomathien/soccer , into r code using dplyr.
The SQL statement is :
SELECT Match.date ,Team....
-1
votes
1
answer
28
views
I need help in R Code to create a new column that chooses a column name given a criteria
I want to create a new column which will choose between the five columns (A,B,C,D,E) and its value will the column name where the value is Maximum. How do I do this with R code?
For instance, I need ...
0
votes
2
answers
33
views
Get a running count of group over time in r
Ok so we have some pretty standard data that looks like this with a date and user id column, but the id can occur multiple times in a day:
id Date
as7fyaisdf 2017-11-08
p98ashdfp9 ...
0
votes
1
answer
18
views
Partial group dataframe by two conditions
Let's say I have the following dataframe, where I have 3 columns (color (Yellow, Green or Red), label (A, B or C) and val(a numeric value)).
For each colour, there is 100 entries. This results in a 3 ...
0
votes
5
answers
26
views
Assign Column Value to Column Names and Pivot it & Group Them
I am sorry for the messy title, I don't know how to describe it. Anyways, this is my current dataframe (1. Dataframe):
# A tibble: 6 x 14
# Groups: Full.Name [3]
Full.Name year `1` ...
1
vote
4
answers
28
views
Calculate the rowwise mean when a maximum number of NA values is given for a set of columns using dplyr
Example dataset...
> tribble(
+ ~colA, ~colB, ~colC, ~colD, ~colE,
+ 1, 2, 3, 4, 5,
+ 2, 3, NA, 4, 5,
+ 3, NA, NA, NA, 4,
+ 4, NA, NA, 5, 6
+ )
# A tibble: 4 × 5
colA colB colC colD ...
0
votes
1
answer
30
views
Compare two columns of distinct data frames and create another column in R
I have the following data frames:
df1
> head(df1)
data.source data.term_name data.term_id data.adjusted_p_value
1 GO:MF catalytic activity GO:0003824 ...
1
vote
1
answer
16
views
Conditional rolling counting function
I would like to implement a rolling count function for the working days in a month. Weekends (Saturday and Sunday) should be assigned a NA.
A replicable example:
#Change language if your are in a non-...
0
votes
1
answer
31
views
Comparing Current Against Previous Values
Back again with a simple issue on paper but struggling with the implementation. For context this is looking at suspects and victims and what we want to achieve is if the current victim is the same as ...
1
vote
2
answers
34
views
Lag dates on condition
I have the following data frame,
Leaflet start_date end_date id
1: 1 2022-01-05 2022-01-08 15
2: 1 2022-01-05 2022-01-11 14
3: 1 2022-01-09 2022-01-11 10
...
0
votes
2
answers
37
views
How to create list of 2 from dataframe?
I have transportation data between UK cities:
from
to
Transit: if there is transit between these two cities = 1,
otherwise =0
weight: average number of passengers
Here is given sample from my ...
2
votes
2
answers
36
views
Restructure data - get mean based on multiple grouping columns
library(tidyverse)
set.seed(10)
dat <- data.frame(age = sample(14:79, size = 15, replace = TRUE),
sex = sample(c("m", "f"), size = 15, replace = TRUE),
...
3
votes
5
answers
48
views
R: calculate groupwise means with overlapping groups
I have the following table with sample data:
set.seed(10)
dat <- data.frame(grp1 = sample(c(0, 1), size = 5, replace = TRUE),
grp2 = sample(c(0, 1), size = 5, replace = TRUE),
...
0
votes
2
answers
22
views
Flag only the first time a column value changes rowwise per id in R
I'll get straight to the point. I found some code on stackoverflow that partially works.
df1 <- read.table(text = "
ID V1 V2
A X SBI123
A Y SBI123
A Y SBI133
B A ...
0
votes
1
answer
25
views
dplyr select() not finding df column when used within function [duplicate]
R noob trying to learn how to clean data with dplyr. I wrote code that cleans up some data and then compares the relationship between one particular variable of interest (level of education) to 4 ...
0
votes
3
answers
26
views
Keep other columns when doing group_by + summarise with dplyr
I want to do a group_by + summarise operation on only two columns with one group attribute while keeping the other three columns unchanged which have the same number for every row. How can I do that? ...
0
votes
1
answer
13
views
Create a column such that - If `x` lies in top 150, its value is "Good". Else its value is "Bad"
How do I create a binary column performance in tbl such that -
If x lies in top 150 (our of 243), value of performance is "Good"
Else, value of performance is "Bad"
tbl <- ...
1
vote
2
answers
29
views
Removing duplicate values from each column of dataframe in R
I need to remove duplicate values from each column of my dataframe. I built this minimum example
library(dplyr)
library(data.table)
df <- data_frame(
temp = c(21, 22, 22, 24, 25, 25, 26, 27),
...
0
votes
1
answer
15
views
Add multiple columns with the same group and sum
I've got this dataframe and I want to add the last two columns to another dataframe by summing them and grouping them by "Full.Name"
# A tibble: 6 x 5
# Groups: authority_dic, Full.Name [6]...
-1
votes
1
answer
17
views
How to do this in Dplyr?
I have this df for example:
And I wish to obtain the following df
How is this possible?
Regards
0
votes
1
answer
29
views
how to find the number of occurrences a unique value appears in a list in R using dplyr?
I extracted data from sql in R. The data is in a list. I want to find the number of occurrences each value appears in a column of the list. How would I go about doing this? I'm very new to R.
...
0
votes
1
answer
22
views
Comparing unordered sets across multiple rows in r
I would like to compare unordered sets of data across multiple rows, identifying the non-matching data from each row into new columns. For instance, my data is structured like
org_id <- c("...
1
vote
3
answers
33
views
How to mutate across a dataframe using dynamic dimension references?
Suppose we start with the data dataframe shown below, with the generating code immediately beneath:
> data
To A B C
1 A 1 3 5
2 B 2 4 6
3 C 4 5 7
4 Sum 7 12 18
library(dplyr)
...
0
votes
1
answer
28
views
How to selectively choose facet_wrap() combinations
I have a data frame with water quality information on 20 lakes. For each lake the ph and dissolved oxygen levels were measured multiple times per visit, and the mean and max values recorded in the ...
1
vote
1
answer
20
views
How to do operations only on specific cells of dataframe in R dplyr
I have data frame below and I want to keep everything unchanged except for the last cell.
I want it to be rounded to the nearest top integer. Is there any way to target one or more specific cells ...
1
vote
2
answers
22
views
Using a condition related to a part of word in r
I have a dataframe like this:
1 2
aquiles_alcatra_Mat_15 picanha
aquiles_alcatra_Mat_15 picanha
alcatra_Mat_15 picanha
alcatra_Mat_20 picanha
alcatra_Mat_25 ...
0
votes
1
answer
16
views
Pivot Longer with separated prefix
I have to make this dataframe Increm_Moran longer in order to make visualisations.
I have basically a common column distance, and then MoranIndex and zscore for 16 variables (community,csrstrategy, + ...
1
vote
1
answer
25
views
Rows until/after last departure from increasing order (last ascending cycle)
I have data akin to multiple timeseries with timestamps. It is organized by group and has a cyclic component in which time increases within some cycles delimited by sudden changes in this increasing ...
0
votes
1
answer
23
views
trying to group_by and then summarize max and min - running into error for unambiguous format
I have addresses that have duplicated information from Kingwood and Humble addresses. I am trying to combine these entries, preserving the the minimum first reported date, and the max last reported ...
0
votes
1
answer
23
views
Divide different groups by reference group
I'm having so far this df: (not column result):
df <- data.frame(number = c(1,1,1,1,2,2,2,2,3,3,3,3),
value1 = c(5,7,6,9,3,5,6,3,4,5,5,6),
group = c("control&...
2
votes
1
answer
57
views
Moving average varying window
I have an unbalanced panel, in which I have certain observations (variable x) per ID and month. I am trying to calculate a 6-month-rolling average of x, but only every March. I know that with zoo, I ...
1
vote
2
answers
34
views
How to choose one row out of several of equal value in a column, depending on the highest value of another column?
So I have a data frame such as this, containing species names, IDs, markers, and size of DNA sequences:
Species | ID| marker| size
-----------------------------------------------------
...
0
votes
1
answer
21
views
how to set the number of lines that will be filtered with {dplyr} in a function?
In an R function, how to set the number of lines that will be filtered with {dplyr} ?
Here's my example.
Many thanks in advance !
library(dplyr)
myfunction <- function(base,
...
1
vote
3
answers
33
views
Find elements in a column that is not in another column of another dataframe in R
I have two dataframes that look something like this:
dat <- data.frame(col1 = c(1:100))
dat2 <- data.frame(col2 = c(5:105))
I want to find all the elements that are in dat but not in dat2. How ...
0
votes
1
answer
27
views
How to refer to column names using a vector in R map / dplyr
I'd like to refer to a column name in a data frame using the quoted (string) version of the column name when I call a function. This is because I want to map the function through various character ...
0
votes
2
answers
20
views
Filter group to include column-wise condition with dplyr
I would like to subset or filter some grouped data in dplyr to include only groups which have 2 different levels of categorical data. My data look like this:
And I would like my output to only ...
0
votes
1
answer
31
views
How can I use multiple stringr functions with mutate & across?
I want to mutate across two or more specified columns and make all strings lower case and replace spaces with an underscore in the same step.
For example...
Starting dataset
> tribble(
+ ~colA, ~...
0
votes
1
answer
30
views
Calculating the slope of the line between two points
I have a question that should be straightforward but I have been really struggling figuring it out.
I have two experimental groups with 10 subjects. For each member I plotted a regression line for the ...