tidyverse

Controlling decimal places displayed in a tibble. Understanding what pillar.sigfig does

一笑奈何 提交于 2021-02-07 13:36:42
问题 I have a csv file weight.csv with the following contents. weight,weight_selfreport 81.5,81.66969147005445 72.6,72.59528130671505 92.9,93.01270417422867 79.4,79.4010889292196 94.6,96.64246823956442 80.2,79.4010889292196 116.2,113.43012704174228 95.4,95.73502722323049 99.5,99.8185117967332 If I do library(readr) Df <- read_csv('weight.csv') Df I get # A tibble: 9 x 2 weight weight_selfreport <dbl> <dbl> 1 81.5 81.7 2 72.6 72.6 3 92.9 93.0 4 79.4 79.4 5 94.6 96.6 6 80.2 79.4 7 116. 113. 8 95.4

tidyverse: row wise calculations by group

纵然是瞬间 提交于 2021-02-07 06:25:40
问题 I am trying to do an inventory calculation in R which requires a row wise calculation for each Mat-Plant combination. Here's a test data set - df <- structure(list(Mat = c("A", "A", "A", "A", "A", "A", "B", "B" ), Plant = c("P1", "P1", "P1", "P2", "P2", "P2", "P1", "P1"), Day = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L), UU = c(0L, 10L, 0L, 0L, 0L, 120L, 10L, 0L), CumDailyFcst = c(11L, 22L, 33L, 0L, 5L, 10L, 20L, 50L)), .Names = c("Mat", "Plant", "Day", "UU", "CumDailyFcst"), class = "data.frame", row

tidyverse: row wise calculations by group

北城以北 提交于 2021-02-07 06:25:12
问题 I am trying to do an inventory calculation in R which requires a row wise calculation for each Mat-Plant combination. Here's a test data set - df <- structure(list(Mat = c("A", "A", "A", "A", "A", "A", "B", "B" ), Plant = c("P1", "P1", "P1", "P2", "P2", "P2", "P1", "P1"), Day = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L), UU = c(0L, 10L, 0L, 0L, 0L, 120L, 10L, 0L), CumDailyFcst = c(11L, 22L, 33L, 0L, 5L, 10L, 20L, 50L)), .Names = c("Mat", "Plant", "Day", "UU", "CumDailyFcst"), class = "data.frame", row

Thoughts on Generating an Age Variable Based on Years

不想你离开。 提交于 2021-02-05 11:30:06
问题 I am trying to create a dummy variable for years. Currently, my data has a birth_date and a program start_date for each observation. I have been able to create a variable measuring an individual's age in days, but what I am actually looking for is a variable: age_join_date that tells me the following: Individual birth_date start_date age_at_join_date A 1990-12-31 2010-12-31 20 yrs old B 1990-12-31 2011-12-31 21 yrs old Essentially what I care about is one's age at the time they joined the

Split Strings into values in long dataframe format [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-05 09:42:02
问题 This question already has answers here : Split comma-separated strings in a column into separate rows (6 answers) Split delimited strings in a column and insert as new rows [duplicate] (6 answers) Closed 3 years ago . I have a dataframe that looks like the following example df which consist of a character variable VAR . df<-data.frame(ID = 1:2, VAR = c("VAL1\r\nVAL2\r\nVAL8","VAL2\r\nVAL5"), stringsAsFactors = FALSE) # ID VAR # 1 1 VAL1\r\nVAL2\r\nVAL8 # 2 2 VAL2\r\nVAL5 I would like to split

tidyverse alternative to left_join & rows_update when two data frames differ in columns and rows

风流意气都作罢 提交于 2021-02-05 08:50:28
问题 There might be a *_join version for this I'm missing here, but I have two data frames, where The merging should happen in the first data frame, hence left_join I not only want to add columns, but also update existing columns in the first data frame, more specifically: replace NA's in the first data frame by values in the second data frame The second data frame contains more rows than the first one. Condition #1 and #2 make left_join fail. Condition #3 makes rows_update fail. So I need to do

R: by group mirrored histogram using ggplot2

五迷三道 提交于 2021-02-05 08:27:10
问题 I would like to get a mirrored histogram by group using ggplot as illustrated in the picture at the bottom. library(ggplot2) data2 <- data.frame( type = c( rep("Top 1", n_segment), rep("Top 2", n_segment), rep("Bottom 1", n_segment), rep("Bottom 2", n_segment)), value = c( rnorm(n_segment, mean=5), rnorm(n_segment, mean=12), rnorm(n_segment, mean=-5), rnorm(n_segment, mean=-12)) ) # Represent it data2 %>% ggplot( aes(x=value, fill=type)) + geom_density( color="#e9ecef", alpha=0.6) For now I

R dplyr filter string condition on multiple columns

99封情书 提交于 2021-02-04 21:47:23
问题 I have a df such as df <-read.table(text=" v1 v2 v3 v4 v5 1 A B X C 2 A B C X 3 A C C C 4 B D V A 5 B Z Z D", header=T) How can I filter variables v2 to v5 if they have an "X". I've seen some examples using filter at but those seem to work only for numeric conditions. filter_at(vars(contains("prefix")), all_vars(.>5)) and replacing >5 for "X" does not work 回答1: You can use filter_at with any_vars to select rows that have at least one value of "X" . library(dplyr) df %>% filter_at(vars(v2:v5),

R dplyr filter string condition on multiple columns

坚强是说给别人听的谎言 提交于 2021-02-04 21:43:29
问题 I have a df such as df <-read.table(text=" v1 v2 v3 v4 v5 1 A B X C 2 A B C X 3 A C C C 4 B D V A 5 B Z Z D", header=T) How can I filter variables v2 to v5 if they have an "X". I've seen some examples using filter at but those seem to work only for numeric conditions. filter_at(vars(contains("prefix")), all_vars(.>5)) and replacing >5 for "X" does not work 回答1: You can use filter_at with any_vars to select rows that have at least one value of "X" . library(dplyr) df %>% filter_at(vars(v2:v5),

R dplyr filter string condition on multiple columns

吃可爱长大的小学妹 提交于 2021-02-04 21:43:29
问题 I have a df such as df <-read.table(text=" v1 v2 v3 v4 v5 1 A B X C 2 A B C X 3 A C C C 4 B D V A 5 B Z Z D", header=T) How can I filter variables v2 to v5 if they have an "X". I've seen some examples using filter at but those seem to work only for numeric conditions. filter_at(vars(contains("prefix")), all_vars(.>5)) and replacing >5 for "X" does not work 回答1: You can use filter_at with any_vars to select rows that have at least one value of "X" . library(dplyr) df %>% filter_at(vars(v2:v5),