Join us in building a kind, collaborative learning community via our updated Code of Conduct.

Questions tagged [dplyr]

Use this tag for questions relating to functions from the dplyr package, such as group_by, summarize, filter, and select.

0
votes
2answers
14 views

Look up and extract values exceeding a threshold in r

I have two data frames: #df1 df1 = data.frame(id = c("A","B","C","D","E"), dev = c(213.5, 225.1, 198.9, 201.0, 266.8)) df1 id dev 1 A 213.5 2 B 225.1 3 C 198.9 4 D 201.0 5 ...
3
votes
3answers
35 views

R dplyr: change the row value of columns having an specific name

I have a data frame. One of the columns has string values that I want to use as a condition for the name of the other columns. For example, df <- data.frame( cond=c("a","b"), aVal=c(1 , 2), ...
1
vote
1answer
17 views

Select columns by regex matching

I know there a lot of posts on this, but I couldn't find any that helped. What I'm trying to do is simple. I want to select (or drop) columns based on whether a letter is present in a column name. ...
1
vote
1answer
23 views

Mutate (dplyr) based on multiple conditions (time intervals)

I'm struggling with conditional merging of the 2 datasets I have. The first one has the following structure ID Trip number Time start Time Stop 1 1 2018-...
0
votes
2answers
23 views

Add specific rows to create new row using R dplyr

I am trying to add a new row Total which would be a sum of apples+bananas+oranges Metric_name May Jun 1 apples 11 34 2 bananas 632 ...
14
votes
2answers
23k views

dplyr filter on Date

my tbl_df: > p2p_dt_SKILL_A%>% + select(Patch,Date,Prod_DL)%>% + head() Patch Date Prod_DL 1 P1 2015-09-04 3.43 2 P11 2015-09-11 3.49 3 P12 2015-09-...
1
vote
0answers
26 views

Converting a data frame to a parameter list for rmarkdown

I’m trying to covert a data frame to a data frame with a list of parameters to send to a parameterized markdown report. I'm following R Markdown: The Definitive Guide as my guide. library(tidyr) ...
1
vote
1answer
34 views

select group before certain observations separated by grouping var in R with NA control

My sample. data=structure(list(add = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,...
1
vote
2answers
30 views

How to automatically refer to names of spreaded columns - dplyr

With the example straight from help: stocks <- data.frame( time = as.Date('2009-01-01') + 0:9, X = rnorm(10, 0, 1), Y = rnorm(10, 0, 2), Z = rnorm(10, 0, 4) ) stocksm <- stocks %>% ...
1
vote
1answer
30 views

Find ties within category in a dataframe ordered by multiple columns

I have a dataframe that looks like: id pdt priority count max 1 1 aaa 0 1 4 2 1 bbb 2 1 4 3 1 ccc 2 1 4 4 1 ddd 2 1 4 5 1 eee 3 ...
0
votes
1answer
35 views

selecting groups with zero values by action column in R

I have next data mydat=structure(list(group = c(111L, 111L, 111L, 111L, 111L, 111L, 111L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 555L, 555L, 555L, 555L, 555L, 555L, 555L), group2 = c(222L, ...
0
votes
3answers
37 views

Stuck counting values using dplyr [duplicate]

I'm struggling with the dplyr-syntax and although I Googled a lot, I´m stuck. I have a data frame with 8,594 rows and two different variables (as factors). Now I want to find out how many times a ...
0
votes
2answers
25 views

Filter argument in function that defaults to make dplyr::filter() filter nothing

I am building a function that imports excel spreadsheets. I would like the function to include an argument that contains names of variables that the user is interested in seeing. The values from the ...
-2
votes
0answers
28 views

R: Summarizing without dropping columns (get an error when using filter) [on hold]

I have dataframes such that the following code executes correctly: varOverTime <- condense %>% group_by(CountryCode) %>% summarize(Variance = var(`Constant Dollars`)) However, the ...
0
votes
0answers
27 views

Dplyr mutate with lag function ignore NA values

I want to create a new column (Ifl_10min_mm.h) on a dataframe with values that are the sum of the previous row and the actual row. I am using dplyr package, with mutate and lag function. When trying ...
0
votes
3answers
27 views

New variable based in non exact number of categoricals values in a tidydata format

Trying to create a new variable based in several categorical conditions in a tidydata. This is an example of my data: d <- data.frame( x = c("a", "a", "b", "b", "b", "c", "c"), y = c("fruit", "...
0
votes
0answers
62 views

getting the number of observations in each group and sub group [on hold]

I am using dplyr to do analyse some data, I have used this chunk of code to group by two factors first by X_pacat1, then by X_bmi5cat, and then get the number of observation under each group for each ...
-4
votes
0answers
37 views

How to find the negative intersection of two dataframes? [duplicate]

I have two dataframes 1 and 2. How can I get the list of subjects which are belong to 1 but not to 2?
-3
votes
0answers
20 views

reshaping Data Frame dplyr [duplicate]

I have a df, need to reshape it. I was planning to do it by spread and count function from dplyr package. Other methods are also welcomed. qw <- c(rep("a",3),"b",rep("c", 2),"a", rep("b", 4)) ql &...
0
votes
1answer
33 views

R - dplyr - mutate_if multiple conditions

I want to mutate a column based on multiple conditions. For example, for each column where the max is 5 and the column name contains "xy", apply a function. df <- data.frame( xx1 = c(0, 1, 2), ...
0
votes
0answers
22 views

How to find means of rows with same names and an additional condition? [duplicate]

I have the dataframe at the following link For each "Session" of each "Subject" I would like to find the mean values of the numeric values in columns like "PP", "HR", "BR" and so on... I would like ...
2
votes
3answers
32 views

Subset from a list and merge together

my.list <- list() for(i in 1:2){ df <- data.frame(year = rep(1980:1981, each = 4), day = rnorm(2*4)) my.list[[i]] <- df } From each element of the list, I want to extract the year and ...
1
vote
1answer
31 views

R - Append rows from dataframe to another one without duplicate on “primary keys columns”

I have to dataframes (A and B). B contains new values and A contains outdated values. Each of these dataframes have one column representing the key and another one representing the value. I want to ...
2
votes
3answers
70 views

R filter on past date given the date in a specific row of data.frame

I have a question related to filtering on dates in R. I found e.g. this link dplyr filter on Date, which answers the question how to filter with help of dplyr in a specific date range. I would like ...
2
votes
2answers
66 views

Parse a string in each row and expand the results into a tidy dataframe

I have a dataframe like: # A tibble: 2 x 3 id text_1 text_2 <int> <chr> ...
2
votes
2answers
33 views

Using a custom function in dplyr process

I have a dataset structured as structure(list(ID = structure(1:26, .Label = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",...
1
vote
1answer
54 views

How to show the maximum value in each row?

I was wondering how I could tell R to only show the maximum values in each rows. For instance, I would like this table: > data<- randu > data[1:10,] x y z 1 0....
0
votes
2answers
54 views

Widen data by group with multiple conditions

I have data on Jenkins Job Pipeline Executions and I am trying to determine the average duration it takes to go from Development to Production based upon the start and end times in the data. The data ...
3
votes
3answers
60 views

Combine select helpers and predicate function to mutate in dplyr

I'd like to programmatically select which columns to mutate, based on a predicate function (e.g. is.character), and a "select helper" (e.g. starts_with("Z")) at the same time. library(dplyr) df <-...
0
votes
2answers
136 views

converting one column to factor works, many doesn't (using dplyr)

got a huge dataframe I want to conver char columns to factor: If I get column by column this way Works: df$column1 <- as.factor(df$column1) class(df$column1) [1] "factor" If I try to automate ...
0
votes
0answers
25 views

Increase size of the boxes and place the legend in a different place

I am using the following code to reproduce the gif attached. library(wbstats) library(dplyr) library(tidyr) library(purrr) library(ggplot2) library(sf) library(rnaturalearth) library(tmap) ...
1
vote
1answer
56 views

Summarise numeric columns, return last value of non-numeric

It's not uncommon to want to summarise numeric columns of a dataframe or tibble, while doing something else to non-numeric columns. There is a nice trick for this here, but it seems to fail for ...
0
votes
1answer
32 views

group_by and selection helpers / standard evaluation [duplicate]

I'm rewriting some Code in a Project where I need to group by (and summarise) a variablename from a character variable, similar to this: test <- mtcars x <- "gear" This would work with ...
0
votes
2answers
49 views

calculation of 90 percentile and replacement it by median by groups in R using 14 and 7 days intervals

This post related with this my post calculation of 90 percentile and replacement of it by median by groups in R solution of jyjek helped me, but not fully. mydat%>% group_by(code,item)%>% ...
-1
votes
1answer
31 views

Does Dplyr groupby Sum all columns or can you specify R [on hold]

New to R and I found groupby but I am curious how I would sum almost all of the columns in my df. a few are text and date but most others are ones I would like to sum. I would like to groupby the ...
0
votes
1answer
33 views

Fill Missing Interval Values in r

I have a data with 4 variables, for which 2 of them are date variables. I would like to check whether the intervals for rows with TYPE == “OT” or TYPE == “NON-OT” fall within the interval of the ...
0
votes
1answer
35 views

dplyr define a temporary variable in mutate function

I am working with dplyr package of R. Let's say I have a data frame of names and ids df <- data.frame(dID=c(1 ,2 , 1 ), name=c("a","a","b")) and I want to resolve each id ...
-3
votes
1answer
44 views

Order dataframe based on inherent values within column

I would like to capture inherent values of within the dataframe and then order the columns and rows from descending to ascending based on the number of events in each column and row. Sample Data #A ...
0
votes
1answer
23 views

R proper usage of `rank` while filtering with dplyr

The following code ranks the iris data frame by sepal length. library(tidyverse) iris %>% filter(rank(Sepal.Length) <= 5) # bottom 5 values iris %>% filter(rank(Sepal.Length) > 5) # ...
5
votes
2answers
84 views

Calculating pvalue within a huge data frame takes very long

I am trying to calculate p.values with a students t-test within a very huge data frame in the long data format. Since my original data frame has about lines within the data frame, the calculation of ...
1
vote
1answer
22 views

Check if string is contained within each vector of a list column in R dataframe

Sorry for the wordy title - I promise when you look at the example below, the title will be clear. I have the following short dataframe: dput(mydf) structure(list(retweet_count = c(186L, 140L, 205L, ...
6
votes
4answers
255 views

rolling regression by group in the tidyverse?

There are many questions about rolling regression in R, but here I am specifically looking for something that uses dplyr, broom and (if needed) purrr. This is what makes this question different. I ...
1
vote
1answer
26 views

R Recode Variables In A Loop

Ciao, Here is a replicate able example. df <- data.frame("STUDENT"=c(1,2,3,4,5), "TEST1"=c(6,88,17,5,18), "TEST2"=c(34,NA,87,88,82), "TEST3"=c(...
1
vote
2answers
27 views

numeric constant/forgot concatenate in r

I forgot to concatenate the finalgrade column and now cannot erase the NA column without getting the phrase Error: unexpected numeric constant in "grades$NA" in anything I try. I do not want to redo ...
0
votes
2answers
23 views

Count string pattern by group in R

I'm trying to obtain a grouped count of a certain string within a dataframe using R, but as of yet have not been able to come up with a solution. Here's some sample data and the code that I've ...
0
votes
1answer
23 views

select group before certain observations separated by grouping var in R

in the continuation of this topic select group before certain observations in R I have grouping var -add (x or y) data=structure(list(add = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,...
0
votes
1answer
43 views

Combining multiple summary statistics in dplyr analysis

For a sample dataframe: df1 <- structure(list(practice = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, ...
1
vote
1answer
85 views

How to collect data from future dplyr query after one fails?

Often when I try to collect a lot of data from a SQL Server database, I get a cryptic error message from the nanodbc driver. For example, library(dplyr) res <- collect(tbl(con, "result")) Error ...
7
votes
1answer
49 views

How to write nested functions with dplyr and dots elipse?

I am trying to put this as simple as possible Some sample data : library(magrittr) library(dplyr) library(rlang) # sample data tib <- tibble( a = 1:3, b = 4:6, c = 7:9 ) Now a function ...
0
votes
2answers
37 views

select group before certain observations in R

Say data=structure(list(x1 = c(88L, 88L, 94L, 82L, 68L, 72L, 43L, 84L, 65L, 91L, 65L, 80L, 82L, 63L, 67L, 58L, 100L, 32L, 75L, 66L, 30L, 12L, 97L, 58L, 14L, 64L), group = structure(c(2L, 2L, 2L, ...