tidyr

dplyr / tidyr - Summarise data with conditions

。_饼干妹妹 提交于 2020-06-21 23:43:32
问题 Problem I am trying to use dyplr & tidyr to achieve an output table (like a contingency table I think) which summarises this data into frequency (eg a count of titles, descriptions & bodies which are negative, neutral and positive numbers). I have tried a number of different methods and the closest example I can find is at Using Tidyr/Dplyr to summarise counts of groups of strings. But this doesn't fit, quite. Example Data The data looks a little like... df <- data.frame( "story_title"=c(0.0

how to transform time codes into turn codes

放肆的年华 提交于 2020-06-17 06:29:37
问题 I want to transform time codes like these library(lubridate) library(tidyverse) df_time <- tibble(time = c(ymd_hms("2020_01_01 00:00:01"), ymd_hms("2020_01_01 00:00:02"), ymd_hms("2020_01_01 00:00:03"), ymd_hms("2020_01_01 00:00:04"), ymd_hms("2020_01_01 00:00:05"), ymd_hms("2020_01_01 00:00:06")), a = c(0, 1, 1, 1, 1, 0), b = c(0, 0, 1, 1, 0, 0)) resulting in >df_time # A tibble: 6 x 3 time a b <dttm> <dbl> <dbl> 1 2020-01-01 00:00:01 0 0 2 2020-01-01 00:00:02 1 0 3 2020-01-01 00:00:03 1 1 4

Purrr safely creating lists of lists

天大地大妈咪最大 提交于 2020-06-13 09:24:33
问题 I've used safely to catch an error which occurs in my code when I'm purring. However, the result from safely is much more complex than I anticipated. First we create the necessary functions and example data. #base functions. SI_tall <- function(topheight, age, si ){ paramasi <- 25 parambeta <- 7395.6 paramb2 <- -1.7829 refAge <- 100 d <- parambeta*(paramasi^paramb2) r <- (((topheight-d)^2)+(4*parambeta*topheight*(age^paramb2)))^0.5 ## height at reference age h2 <- (topheight+d+r)/ (2+(4

How can I keep pivot_wider() from dropping factor levels in names?

时光总嘲笑我的痴心妄想 提交于 2020-06-12 07:14:49
问题 I would really like pivot_wider to create a column with NAs if the level of a factor exists but never appears in the data when it's used as a names_from argument. For example, the first line gives me a two column tibble, but I'd really like the three column tibble below. tibble(Person=c("Sarah", "Jackson", "Jackson"), Rank=c(1,1,2), FavoriteAnimal=factor(c("Dog", "Dog", "Cat")))%>% group_by(Person)%>%arrange(Rank)%>%slice(1)%>% pivot_wider(names_from = FavoriteAnimal, values_from=Rank) tibble

BatchGetSymbols - reshape output

不羁的心 提交于 2020-06-09 05:20:53
问题 I like to use the advanted of BatchgetSymbols. Any advice how I can best manipulate the output to receive the format below? symbols_RP <- c('VDNR.L','VEUD.L','VDEM.L','IDTL.L','IEMB.L','GLRE.L','IGLN.L') #Setting price download date range from_date <- as.Date('2019-01-01') to_date <- as.Date(Sys.Date()) get.symbol.adjclose <- function(ticker) { l.out <- BatchGetSymbols(symbols_RP, first.date = from_date, last.date = to_date, do.cache=TRUE, freq.data = "daily", do.complete.data = TRUE, do.fill

tidyr::unnest() with different column types

故事扮演 提交于 2020-05-15 10:15:42
问题 Since the update to tidyr version 1.0.0 I have started to get an error when unnesting a list of dataframes. The error comes because some of the data frames in the list contain a column with all NA values (logical), while other of the dataframes contain the same column but with some character values (character). The columns with all NA values are coded as logicals while the others are coded as character vectors. The default behavior of earlier versions of tidyr handled the different column

tidyr::unnest() with different column types

你。 提交于 2020-05-15 10:14:05
问题 Since the update to tidyr version 1.0.0 I have started to get an error when unnesting a list of dataframes. The error comes because some of the data frames in the list contain a column with all NA values (logical), while other of the dataframes contain the same column but with some character values (character). The columns with all NA values are coded as logicals while the others are coded as character vectors. The default behavior of earlier versions of tidyr handled the different column

How to create a formulated table in R?

吃可爱长大的小学妹 提交于 2020-05-14 11:57:57
问题 This is my reproducible example : #http://gekkoquant.com/2012/05/26/neural-networks-with-r-simple-example/ library("neuralnet") require(ggplot2) traininginput <- as.data.frame(runif(50, min=0, max=100)) trainingoutput <- sqrt(traininginput) trainingdata <- cbind(traininginput,trainingoutput) colnames(trainingdata) <- c("Input","Output") Hidden_Layer_1 <- 1 # value is randomly assigned Hidden_Layer_2 <- 1 # value is randomly assigned Threshold_Level <- 0.1 # value is randomly assigned net.sqrt

How to create a formulated table in R?

最后都变了- 提交于 2020-05-14 11:56:29
问题 This is my reproducible example : #http://gekkoquant.com/2012/05/26/neural-networks-with-r-simple-example/ library("neuralnet") require(ggplot2) traininginput <- as.data.frame(runif(50, min=0, max=100)) trainingoutput <- sqrt(traininginput) trainingdata <- cbind(traininginput,trainingoutput) colnames(trainingdata) <- c("Input","Output") Hidden_Layer_1 <- 1 # value is randomly assigned Hidden_Layer_2 <- 1 # value is randomly assigned Threshold_Level <- 0.1 # value is randomly assigned net.sqrt

How to pivoting dataframe consisting column with section and sub section In R

我是研究僧i 提交于 2020-05-10 04:11:17
问题 I have a below-mentioned dataframe: structure( list(ID = c("P-1", " P-1", "P-1", "P-2", "P-3", "P-4", "P-5", "P-6", "P-7", "P-8"), Date = c("2020-03-16 12:11:33", "2020-03-16 13:16:04", "2020-03-16 06:13:55", "2020-03-16 10:03:43", "2020-03-16 12:37:09", "2020-03-16 06:40:24", "2020-03-16 09:46:45", "2020-03-16 12:07:44", "2020-03-16 14:09:51", "2020-03-16 09:19:23"), Status = c("SA", "SA", "SA", "RE", "RE", "RE", "RE", "XA", "XA", "XA"), Flag = c("L", "L", "L", NA, "K", "J", NA, NA, "H", "G"