mutate

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

南笙酒味 提交于 2019-12-25 10:12:45
问题 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

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

岁酱吖の 提交于 2019-12-25 10:11:09
问题 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

Recalculate the new weighted mean when merging two factors by group, and keep original data

有些话、适合烂在心里 提交于 2019-12-24 19:38:34
问题 I'm working on a data frame that contains: counts per cluster (flow cytometry data) of several files and mean, max, min, total for lots of variables recorded by the machine. In the case that I want to reduce the number of groups (pool similar clusters together) I would want to merge all the information in a file for group 'a' and 'b' by file So far, following this SO Question I have already worked out the min, max, and total, but am stuck on how to get the following calculation working in

Generate self reference key within the table using R mutate in a dataframe

最后都变了- 提交于 2019-12-24 15:23:04
问题 I have an input table with 3 columns (Person_Id, Visit_Id (unique Id for each visit and each person) and Purpose) as shown below. I would like to generate another new column which provides the immediate preceding visit of the person (ex: if person has visited hospital with Visit Id = 2, then I would like to have another column called "Preceding_visit_Id" which will be 1 (ex:2, if visit id = 5, preceding visit id will be 4). Is there a way to do this in a elegant manner using mutate function?

Mutate all numeric variables with condition

有些话、适合烂在心里 提交于 2019-12-24 07:25:19
问题 I have the following dataset. structure(list(Monate = structure(c(5L, 4L, 9L, 1L, 8L, 7L, 6L, 2L, 12L, 11L, 10L, 3L), .Label = c("April", "August", "Dezember", "Februar", "Januar", "Juli", "Juni", "Mai", "März", "November", "Oktober", "September"), class = "factor"), `06:00:00-00:59:59` = c(1.23650890285152, 1, 0.941959539059369, 0.89618038635695, 0.839845976370379, 0.75789368475892, 0.745357979199247, 0.72726835342115, 0.852384444043199, 0.909567524854388, 0.927385464132519, 0.86987613403725

How to obtain species richness and abundance for sites with multiple samples using dplyr

℡╲_俬逩灬. 提交于 2019-12-24 00:56:57
问题 Problem: I have a number of sites, with 10 sampling points at each site. Site Time Sample Species1 Species2 Species3 etc Home A 1 1 0 4 ... Home A 2 0 0 2 ... Work A 1 0 1 1 ... Work A 2 1 0 1 ... Home B 1 1 0 4 ... Home B 2 0 0 2 ... Work B 1 0 1 1 ... Work B 2 1 0 1 ... ... I would like to obtain the richness and abundance of each site. Richness is the total number of species at a site, and abundance is the total number of all individuals of all species at a site, like this: Site Time

Recode multiple columns using dplyr

人走茶凉 提交于 2019-12-23 13:08:52
问题 I had a dataframe where I recoded several columns so that 999 was set to NA dfB <-dfA %>% mutate(adhere = if_else(adhere==999, as.numeric(NA), adhere)) %>% mutate(engage = if_else(engage==999, as.numeric(NA), engage)) %>% mutate(quality = if_else(quality==999, as.numeric(NA), quality)) %>% mutate(undrstnd = if_else(undrstnd==999, as.numeric(NA), undrstnd)) %>% mutate(sesspart = if_else(sesspart==999, as.numeric(NA), sesspart)) %>% mutate(attended = if_else(attended>=9, as.integer(NA),

Using dplyr mutate with conditions based on multiple columns

空扰寡人 提交于 2019-12-23 04:49:08
问题 Without NAs, the following code would work as intended: if the first row has any 2's in it, the new variable takes a value of 2; if not, I want to check if any of the values are 1; if not, check if any are 0; if not, then all must be NA. Once I add NAs into the data frame, it no longer works and I can't seem to figure out why: V1 <- c(NA,1,2,0,0) V2 <- c(0,0,2,1,1) V3 <- c(NA,0,2,1,0) V <- cbind(V1,V2,V3) V <- mutate(V,V4 = ifelse(V1 == 2|V2==2|V3==2, 2, ifelse(V1==1|V2==1|V3==1, 1, ifelse(V1

Return list using mutate and rowwise

橙三吉。 提交于 2019-12-22 18:42:39
问题 I'm trying to return a list using mutate and rowwise but get the error shown in the code. These questions Q1 Q2 helped, but I'd like to keep it simple by iterating over rows using rowwise() , and the questions are 3yr 7mth old. Thanks. library(tidyverse) df <- data.frame(Name=c("a","a","b","b","c"),X=c(1,2,3,4,5), Y=c(2,3,4,2,2)) TestFn <- function(X,Y){ Z <- list(X*5,Y/2,X+Y,X*2+5*Y) return (Z) } #this works SingleResult <- TestFn(5,20) #error - Error in mutate_impl(.data, dots) :

Error casted by simple mutate using tidyverse or dplyr

别来无恙 提交于 2019-12-21 20:43:22
问题 I am having serious troubles using the tidyverse package that I cannot debug. As an example, "mutate" does not work properly even on past project I have already produced. This all started when I installed the following package: library(pdftools) library(tm) library(stringi) library(tidyverse) (or library(dplyr) library(tidyr)) library(purrr) ) And it still remains when I do a rm(list=ls()) . The only thing I haven't tried so forth is deinstalling R/RStudio and reinstalling it. I use RStudio