sapply

in R dplyr why do I need to ungroup() after I count()?

本秂侑毒 提交于 2020-06-25 09:09:28
问题 When I first started programming in R I would often use dplyr count() . library(tidyverse) mtcars %>% count(cyl) Once I started using apply functions I started running into issues with count() . If I simply added ungroup() to the end of my count() 's the problems would go away. I don't have any particular reproducibles to show. But can somebody explain what the issue likely was, why ungroup() always fixed it, and are there any drawbacks to consistently using ungroup() after every count() , or

in R dplyr why do I need to ungroup() after I count()?

只谈情不闲聊 提交于 2020-06-25 09:07:20
问题 When I first started programming in R I would often use dplyr count() . library(tidyverse) mtcars %>% count(cyl) Once I started using apply functions I started running into issues with count() . If I simply added ungroup() to the end of my count() 's the problems would go away. I don't have any particular reproducibles to show. But can somebody explain what the issue likely was, why ungroup() always fixed it, and are there any drawbacks to consistently using ungroup() after every count() , or

Convert a list of data frames into a single data frame with list name [duplicate]

旧街凉风 提交于 2020-03-03 08:56:27
问题 This question already has answers here : Simultaneously merge multiple data.frames in a list (8 answers) Closed 15 days ago . I am hoping to determine an efficient way to convert a list of data frames into a single data frame. Below is my reproducible MWE: set.seed(1) ABAge = runif(100) ABPoints = rnorm(100) ACAge = runif(100) ACPoints = rnorm(100) BCAge = runif(100) BCPoints = rnorm(100) A_B <- data.frame(ID = as.character(paste0("ID", 1:100)), Age = ABAge, Points = ABPoints) A_C <- data

Correct use of sapply with Anova on multiple subsets in R

孤街醉人 提交于 2020-02-03 02:07:48
问题 I am trying to run a two-way ANOVA on multiple subsets of a data frame without having to actually subset the data as this is in-efficient Example data: DF<-structure(list(Sample = c(666L, 676L, 686L, 667L, 677L, 687L, 822L, 832L, 842L, 824L, 834L, 844L), Time = c(300L, 300L, 300L, 300L, 300L, 300L, 400L, 400L, 400L, 400L, 400L, 400L), Ploidy = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2n", "3n"), class = "factor"), Tissue = c("muscle", "muscle", "muscle", "liver

Correct use of sapply with Anova on multiple subsets in R

删除回忆录丶 提交于 2020-02-03 02:07:20
问题 I am trying to run a two-way ANOVA on multiple subsets of a data frame without having to actually subset the data as this is in-efficient Example data: DF<-structure(list(Sample = c(666L, 676L, 686L, 667L, 677L, 687L, 822L, 832L, 842L, 824L, 834L, 844L), Time = c(300L, 300L, 300L, 300L, 300L, 300L, 400L, 400L, 400L, 400L, 400L, 400L), Ploidy = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2n", "3n"), class = "factor"), Tissue = c("muscle", "muscle", "muscle", "liver

How do I add rows to a data frame using sapply?

て烟熏妆下的殇ゞ 提交于 2020-01-30 10:21:06
问题 I'm trying to add rows to a data frame within an sapply function call, but it's returning a matrix, whereas I want a data frame with variables in the columns and names/addresses in the rows. This is a trivial example that demonstrates the problem. I'm doing it this way to avoid using a 'for' loop. Can someone please teach me how I should be doing this? # initialize and empty data frame absdf <- data.frame( name = character(), address = character(), city = character(), stringsAsFactors=FALSE)

R: how to avoid strsplit hiccuping on empty vectors when splitting text

妖精的绣舞 提交于 2020-01-16 12:00:19
问题 Have a list of text- sections which are required to be split into sentences by: > textList <- list(sections=sections[(length(sections)-2):length(sections)]) > textList$sentences <- sapply(textList$sections, function(x) strsplit(as.character(x), "(?<=und/KON)\\s(?!\\S+/V)|(?<=oder/KON)\\s|(?<=/\\$[[:punct:]])\\s(?!dass/KOUS)(?!dann/ADV)(?!weil/KOUS)", perl=TRUE)) > sent <- textList$sentences The final goal is to add ID s to all sentences and arrange them together into a list of dataframes -

R: how to avoid strsplit hiccuping on empty vectors when splitting text

徘徊边缘 提交于 2020-01-16 11:59:10
问题 Have a list of text- sections which are required to be split into sentences by: > textList <- list(sections=sections[(length(sections)-2):length(sections)]) > textList$sentences <- sapply(textList$sections, function(x) strsplit(as.character(x), "(?<=und/KON)\\s(?!\\S+/V)|(?<=oder/KON)\\s|(?<=/\\$[[:punct:]])\\s(?!dass/KOUS)(?!dann/ADV)(?!weil/KOUS)", perl=TRUE)) > sent <- textList$sentences The final goal is to add ID s to all sentences and arrange them together into a list of dataframes -

Using sapply on date/factor vector field - include incrementing value

╄→尐↘猪︶ㄣ 提交于 2020-01-15 05:27:24
问题 I have a date field (factor class converted to string) with missing values that I would like to fill with sequencing numbers for every missing value. Here is my code so far... f<- function(x, counter){ if(x == ""){ counter = counter + 1; return (toString(counter)) } else{ return (toString(x)) } } sapply(x$DateTime_, f, -9999) The counter does not increment and returns a vector like: [1] "-9998" "-9998" "-9998" "-9998" "-9998" "1/1/1998" Any help to get the counter to increment would be