Transpose specified columns columns to rows with grouped data

I have a dataframe like this: household person R01 R02 R03 R04 R05 1 1 1 NA 1 7 7 NA 2 1 2 1 NA 7 7 NA 3 1 3 3 3 NA 11 NA 4 1 4 3 3 11 NA NA 5 2 1 NA 7 16 NA NA 6 2… Read More Transpose specified columns columns to rows with grouped data

If I have two groups in my data which partially match for a second categorical variable, is there a way to remove non-matching groups?

For example, I have two datasets (A,B), which have a score column, a location column (England or Wales) and a month column. If data set A only has the months January through to October, while data set B only has the months April – November, is there a way to filter my data to only… Read More If I have two groups in my data which partially match for a second categorical variable, is there a way to remove non-matching groups?

rewriting `summarise_all` without deprecated `funs`, using Simple list and Auto-named list

I’m trying to count the number of NA values in each of 2 columns. The code below works. temp2 %>% select(c18basic, c18ipug) %>% summarise_all(funs(sum(is.na(.)))) But I get this warning: Warning message: `funs()` was deprecated in dplyr 0.8.0. Please use a list of either functions or lambdas: # Simple named list: list(mean = mean, median =… Read More rewriting `summarise_all` without deprecated `funs`, using Simple list and Auto-named list

How to remove pairs of rows corresponding to same value in R dataframe?

For the unique pair of ID if both corresponding rows are 0, I need to remove them. In this case, remove row#5 and 6 but not row #7 and #8 tmt.pair <- c("A","A","A","A","B","B","B","B") tmt <- c("1000 C","4000 C","1000 C","4000 C","1000 C","4000 C","1000 C","4000 C") year <- c("2021","2021","2021","2021","2021","2021","2020","2020") month <- c("A","A","A","A","J","J","O","O") level <- c("Low","Low","Up","Up","Low","Low","Low","Low") site <-… Read More How to remove pairs of rows corresponding to same value in R dataframe?

R Removing words from a string in a dataframe

Let’s say I have the following dataset: Date_Received = c("Addition 1/2/2018", "Swimming Pool 1/8/2018", "Abandonment 1/9/2018", "Existing Approval 3/14/2018", "Holding Tank 5/11/2018") Date_Approved = c("1/2/2018", "1/8/2018", "1/9/2018", "SB 3/21/2018", "JW 5/11/2018") And I want to removed the characters before the date in the Date_Received column, so that I can later convert it to date type… Read More R Removing words from a string in a dataframe

collapse duplicate rows into a single row by "|"

I have a data frame which looks like: # A tibble: 19 × 5 Date Country `Implementing Institution(s)` `Policy Tool(s)` Magnitude <date> <chr> <chr> <chr> <chr> 1 2020-05-14 Spain Ministry of Economic Affairs and Digital Transformation,European Investment Bank Lending 1.5 2 2020-05-28 Spain European Investment Bank Lending 250 3 2020-06-16 Spain European Investment Bank Credit… Read More collapse duplicate rows into a single row by "|"

Calculate % Change Avoiding NA's, By Group

I have the following sample data: df <- structure(list(person = c("a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "c", "c", "c", "c", "c", "c", "c", "d", "d", "d", "e", "f"), data = c(4.51646448148296, 6.50396548317118, NA, 9.84425670640439, NA, NA, 1.67850554309375, NA, 2.06950894895984, 4.69548965710482, 5.12795074939009, 0.209845957108408, 6.47290211779118, 6.26481009615426, NA, NA, 5.24543093106204, 4.75242871394851, 6.8182202925863, NA,… Read More Calculate % Change Avoiding NA's, By Group

Calculating growth rates across rows by groups in the long format (relative to a base group) in R

I would like to calculate growth rates across rows by groups (here groups are "country" and "year") in a dataset that is in the long format. Since the growth rate should be relative to the same value (i.e., same "country" and same "year") under the "Baseline" scenario (not relative to the previous row), I am… Read More Calculating growth rates across rows by groups in the long format (relative to a base group) in R

Why doesn't R dplyr arrange sort properly using a vector element within a for loop

I’m having trouble getting r’s dplyr::arrange() to sort properly when used in a for loop. I found many posts discussing this issue (like ex.1 with the .by_group=TRUE and using desc() bettter, ex.2 with lists, and ex.3 with filter_all() and %in%). Yet, I’m still having a bit of trouble understanding why I can get the arrange()… Read More Why doesn't R dplyr arrange sort properly using a vector element within a for loop