Advertisements I am trying to add a totals row usin ghte Janitor package in R, however I need 2 columns to be totaled using the sum function and one column to be a percentage (not the sum of the column). library(tidyverse) library(janitor) df.1 <- tribble( ~customer ,~period, ~cost1, ~cost2 , ‘cust1’, ‘202201’, 5, 10, ‘cust1’,… Read More Specify the calculation by column using adorn_totals()
Advertisements The tabyl function from the janitor package generates a frequency table of the specified variables. By default, it excludes missing values from the output. Let’s say I have the following dataframe: df <- data.frame( var_1 = c(rep("ja",7),rep("nein",13)), var_2 = c(31:50), var_3 = c(rep(0,10),rep(1,10))) Then the output of tabyl will be like: library(janitor) df %>%… Read More Force tabyl to show even columns with all 0 as values
Advertisements I have responses where I solicited rankings of issues and the response I have for each individual look like this: structure(list(Rank = "Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);Economic impacts;Climate change;"), row.names = c(NA, -1L), class = "data.frame") I am trying to create a column that gives the… Read More Creating a col that adds a numeric value based on the order of a character string in R
Advertisements for dataframe below, df <- data.frame(id = c(rep(101, 4), rep(202, 3)), status = c("a","b","c","d", "a", "b", "c"), wt = c(100,200,100,105, 20,22,25), ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1)) df id status wt ht 1 101 a 100 5.3 2 101 b 200 5.2 3 101 c 100 5.0 4 101 d 105 5.1 5 202 a 20… Read More How to insert rows in specific indices of dataframe containing sum of few rows above only in R pipe dplyr
Advertisements I’ve been tasked with returning only rows with unique IDs but returning a row for every ID in SQL. How would I go about this? Logic: For primary row, select where JOB_INDICATOR = ‘P’. If there are multiple rows, then use the record where PRIM_ROLE_IND = ‘Y’. If there are still multiple then select… Read More How do I conditionally select a unique value in SQL?
Advertisements I have two columns Project ID and Project Number in my dataframe hpds_all_clean with NAs in them, and I’m trying to replace all NAs occurrences with "Undisclosed Project ID" and "Undisclosed Project Number". Here are some of my attempts which have the same error: target of assignment expands to non-language object Using gsub(): hpds_all_clean… Read More Replace all occurrences of columns in a data frame