mutate

Mutate usage length of column error

假装没事ソ 提交于 2019-12-08 17:22:29
I'm a new R user. I've been trying to compute the time difference between two time points, and calculate a percentage value (habitual_efficiency). Using this percentage value, I need to assign specific value to a new variable (i). time_difference1 <- as.numeric(difftime(strptime(var$psqi_1_bedtime, "%H:%M"), strptime(var$psqi_3_waketime, "%H:%M"))) timedifference <- abs (24 - time_difference1) var4 <- data.frame (habitual_efficiency = 1: nrow(var)) transmute (var, habitual_efficiency = (var$psqi_4_sleeptime*100/timedifference)) var4 <- data.frame (comp4score = 1: nrow(var)) var4 <- var4 %>%

Sorting a column based on the order of another column in R

无人久伴 提交于 2019-12-08 07:50:57
问题 The R script below creates a data frame a123 with three columns. Column a1 has three variables occurring at different places with corresponding a2 and a3 values. a1 = c("A", "B", "C", "A", "B", "B", "A", "C", "A", "C", "B") a2 = c( 10, 8, 11 , 6 , 4 , 7 , 9 , 1 , 3 , 2, 7) a3 = c( 55, 34, 33, 23, 78, 33, 123, 34, 85, 76, 74) a123 = data.frame(a1, a2, a3) My need is that I want a3 column values corresponding to a1 column values to be arranged in ascending order based on the order of a2 values.

Mutate usage length of column error

狂风中的少年 提交于 2019-12-08 06:27:54
问题 I'm a new R user. I've been trying to compute the time difference between two time points, and calculate a percentage value (habitual_efficiency). Using this percentage value, I need to assign specific value to a new variable (i). time_difference1 <- as.numeric(difftime(strptime(var$psqi_1_bedtime, "%H:%M"), strptime(var$psqi_3_waketime, "%H:%M"))) timedifference <- abs (24 - time_difference1) var4 <- data.frame (habitual_efficiency = 1: nrow(var)) transmute (var, habitual_efficiency = (var

Using mutate_at to create new columns by using other columns in the same data frame

风流意气都作罢 提交于 2019-12-07 20:35:17
问题 Okey, I'm having trouble with something that should be simple. If I have a dataframe like this: x <- data.frame(a = seq(1,3), b = seq(2,4), c = seq(3,5), d = seq(4,6), b2 = seq(5,7), c2 = seq(6,8), d2 = seq(7,9)) # a b c d b2 c2 d2 # 1 2 3 4 5 6 7 # 2 3 4 5 6 7 8 # 3 4 5 6 7 8 9 I want to use mutate_at to create new columns based on the result from b/b2, c/c2 etc. When I try: myvars <- c(2:4) dvars <- c(5:7) x <- x %>% mutate_at(vars(myvars), funs('_new' = vars(myvars) / vars(dvars))) I get

mutate_at evaluation error when using group_by

荒凉一梦 提交于 2019-12-07 17:53:48
问题 mutate_at() shows an evaluation error when used with group_by() and when imputing a numerical vector for column position as the first (.vars) argument. Issue shows up when using R 3.4.2 and dplyr 0.7.4 version Works fine when using R 3.3.2 and dplyr 0.5.0 Works fine if .vars is character vector (column name) Example: # Create example dataframe Id <- c('10_1', '10_2', '11_1', '11_2', '11_3', '12_1') Month <- c(2, 3, 4, 6, 7, 8) RWA <- c(0, 0, 0, 1.579, NA, 0.379) dftest = data.frame(Id, Month,

Applying group_by and summarise(sum) but keep columns with non-relevant conflicting data?

微笑、不失礼 提交于 2019-12-07 01:34:22
问题 My question is very similar to Applying group_by and summarise on data while keeping all the columns' info but I would like to keep columns which get excluded because they conflict after grouping. Label <- c("203c","203c","204a","204a","204a","204a","204a","204a","204a","204a") Type <- c("wholefish","flesh","flesh","fleshdelip","formula","formuladelip", "formula","formuladelip","wholefish", "wholefishdelip") Proportion <- c(1,1,0.67714,0.67714,0.32285,0.32285,0.32285, 0.32285, 0.67714,0.67714

Return list using mutate and rowwise

让人想犯罪 __ 提交于 2019-12-06 09:01:15
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) : incompatible size (4), expecting 1 (the group size) or 1 dfResult <- df %>% rowwise() %>% mutate(R=TestFn(X,Y))

mutate_at evaluation error when using group_by

妖精的绣舞 提交于 2019-12-05 23:17:13
mutate_at() shows an evaluation error when used with group_by() and when imputing a numerical vector for column position as the first (.vars) argument. Issue shows up when using R 3.4.2 and dplyr 0.7.4 version Works fine when using R 3.3.2 and dplyr 0.5.0 Works fine if .vars is character vector (column name) Example: # Create example dataframe Id <- c('10_1', '10_2', '11_1', '11_2', '11_3', '12_1') Month <- c(2, 3, 4, 6, 7, 8) RWA <- c(0, 0, 0, 1.579, NA, 0.379) dftest = data.frame(Id, Month, RWA) # Define column to fill NAs nacol = c('RWA') # Fill NAs with last period dftest_2 <- dftest %>%

use dplyr mutate() in programming

╄→尐↘猪︶ㄣ 提交于 2019-12-04 14:06:50
问题 I am trying to assign a column name to a variable using mutate. df <-data.frame(x = sample(1:100, 50), y = rnorm(50)) new <- function(name){ df%>%mutate(name = ifelse(x <50, "small", "big")) } When I run new(name = "newVar") it doesn't work. I know mutate_() could help but I'm struggling in using it together with ifelse . Any help would be appreciated. 回答1: Using dplyr 0.7.1 and its advances in NSE, you have to UQ the argument to mutate and then use := when assigning. There is lots of info on

Error casted by simple mutate using tidyverse or dplyr

三世轮回 提交于 2019-12-04 13:50:10
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 version 1.0.153 and R version 3.4.1. I actually tried to reproduce the bug on other computers and this