tidyverse

using purrr to affect single columns of each dataframe in a list

余生长醉 提交于 2020-01-02 02:03:37
问题 still getting used to purrr and I have one of those questions that I think should be easy, but I don't know how to do it. All I want to do is convert the datetimes in the below, to dates with as.Date(). it's a list of dataframes. Been playing around but haven't found something that works yet... any help appreciated. df <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(24), by = "hour"), useless = "ignore me") df2 <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(1),

using purrr to affect single columns of each dataframe in a list

耗尽温柔 提交于 2020-01-02 02:03:32
问题 still getting used to purrr and I have one of those questions that I think should be easy, but I don't know how to do it. All I want to do is convert the datetimes in the below, to dates with as.Date(). it's a list of dataframes. Been playing around but haven't found something that works yet... any help appreciated. df <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(24), by = "hour"), useless = "ignore me") df2 <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(1),

bunch recoding of variables in the tidyverse (functional / meta-programing)

血红的双手。 提交于 2020-01-01 19:39:11
问题 I want to recode a bunch of variables with as few function calls as possible. I have one data.frame where I want to recode a number of variables. I create a named list of all variable names and the recoding arguments I want to execute. Here I have no problem using map and dpylr . However, when it comes to recoding I find it much easier using recode from the car package, instead of dpylr 's own recoding function. A side question is whether there is a nice way of doing the same thing with dplyr

Combining Multiple Columns with Tidyr's Unite by Referencing Similar Column Names

夙愿已清 提交于 2019-12-30 10:46:20
问题 library(tidyr) library(dplyr) library(tidyverse) Below is the code for a simple dataframe. I have some messy data that was exported with column factor categories spread out in different columns. Client<-c("Client1","Client2","Client3","Client4","Client5") Sex_M<-c("Male","NA","Male","NA","Male") Sex_F<-c(" ","Female"," ","Female"," ") Satisfaction_Satisfied<-c("Satisfied"," "," ","Satisfied","Satisfied") Satisfaction_VerySatisfied<-c(" ","VerySatisfied","VerySatisfied"," "," ")

How to unnest column-list?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-29 07:09:16
问题 I have a tibble like: tibble(a = c('first', 'second'), b = list(c('colA' = 1, 'colC' = 2), c('colA'= 3, 'colB'=2))) # A tibble: 2 x 2 a b <chr> <list> 1 first <dbl [2]> 2 second <dbl [2]> Which a would like to turn into this form: # A tibble: 2 x 4 a colA colB colC <chr> <dbl> <dbl> <dbl> 1 first 1. NA 2. 2 second 3. 2. NA I tried to use unnest() , but I am having issues preserving the elements' names from the nested values. 回答1: You can do this by coercing the elements in the list column to

Why reassigning new name to dataframe in dplyr makes it faster?

南笙酒味 提交于 2019-12-25 18:34:52
问题 I was unhappy with the time dplyr and data.table were taking to create a new variable on my data.frame and decide to compare methods. To my surprise, reassigning the results of dplyr::mutate() to a new data.frame seems to be faster than not doing so. Why is this happening? library(data.table) library(tidyverse) dt <- fread(".... data.csv") #load 200MB datafile dt1 <- copy(dt) dt2 <- copy(dt) dt3 <- copy(dt) a <- Sys.time() dt1[, MONTH := month(as.Date(DATE))] b <- Sys.time(); datatabletook <-

R: Unlist list to columns in dataframe

和自甴很熟 提交于 2019-12-25 18:25:56
问题 I have a data frame which is made of up: Var1 Var2 Var3 a b nested list Nested list = ####test: apple: a pear: b ####test2: apple: a orange: c Is there a simple way to turn the nested list in v3 of each row into further columns, with the list element names as the variable names and each list value in each cell? e.g. Var1 Var2 Var3 apple pear orange a b nested list a b NULL 回答1: maybe you are looking for tidyr::spread() . It is anyways hard to try other things without a reproducible example.

R - Filter 1st Dataframe with conditions of timestamp from DF2

北城余情 提交于 2019-12-25 18:25:01
问题 DF1: X Y DateTime 1 113.8591 22.25272 2016-01-07 10:37:33 2 113.8585 22.25276 2016-01-07 10:37:43 3 113.8578 22.25270 2016-01-07 10:37:53 4 113.8572 22.25265 2016-02-01 11:34:03 5 113.8565 22.25260 2016-02-18 12:20:13 6 113.8559 22.25251 2016-02-18 12:20:23 structure(list(Date = c("2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016

Remove duplicates by multiple conditions

只谈情不闲聊 提交于 2019-12-25 13:43:27
问题 I have data where an individual (Name) appears multiple times in a eggphase category. I would like for there only to be one sample per individual but I don't just want to keep the first one the R finds. I would like to keep the one where the group appears most in all other categories. Hopefully my example helps make this clear. library(tidyverse) myDF <- read.table(text="Tissue Food Eggphase Name Group wb fl after Kia a wb fl after Kia c wb wf before Kia b wb fl before Lucy c wb fl after Lucy

tidyr; %>% group_by() mutate(foo = fill() )

冷暖自知 提交于 2019-12-25 10:16:35
问题 I'm struggling to create a new variable to indicate what letter, LET , some groups, grp , within id, id , begin with. In the following I'll illustrate my question. I have data like this, library(dplyr); library(tidyr) df <- tibble(id = rep(0:1, c(7, 10)), grp = rep(c(0,1,0,1,2), c(3,4,2,5,3)), LET = rep(c('A', 'B', 'A', 'B', 'A', 'B'), c(1,4, 3, 3, 4, 2))) #> # A tibble: 17 x 3 #> id grp LET #> <int> <dbl> <chr> #> 1 0 0 A #> 2 0 0 B #> 3 0 0 B #> 4 0 1 B #> 5 0 1 B #> 6 0 1 A #> 7 0 1 A #> 8