logo
down
shadow

Write a function with default column name inputs in dplyr::mutate()


Write a function with default column name inputs in dplyr::mutate()

Content Index :

Write a function with default column name inputs in dplyr::mutate()
Tag : r , By : m0gb0y74
Date : November 27 2020, 04:01 AM

hop of those help? We can write a function called round_x() that wraps around mutate() and has age as a default argument:
library(dplyr)

round_x <- function(.data, x = age) {
  x <- enquo(x)
  var_name <- paste0("round_", quo_name(x))
  mutate(.data, !!var_name := round(!!x))
}
data %>% round_x()
#   age round_age
#1 50.1        50
#2 60.5        60
data.frame(data, weight = c(180.5, 200.6)) %>% round_x(weight)
#   age weight round_weight
#1 50.1  180.5          180
#2 60.5  200.6          201

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Dplyr write a function with column names as inputs


Tag : r , By : user185939
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I'm writing a function that I'm going to use on multiple columns in dplyr, but I'm having trouble passing column names as inputs to functions for dplyr. , Is this what you expected?
df<-tbl_df(data.frame(group=rep(c("A", "B"), each=3), var1=sample(1:100, 6), var2=sample(1:100, 6)))

example<-function(colname){
  df %>%
    group_by(group)%>%
    summarize(output=mean(sqrt(colname)))%>%
    select(output)
}
example( quote(var1) )
#-----
Source: local data frame [2 x 1]

    output
1 7.185935
2 8.090866

Why can't I apply a function to create a new column with mutate() using dplyr?


Tag : r , By : Edwin
Date : March 29 2020, 07:55 AM
will be helpful for those in need I have a data.frame, let's call it " df". , As pointed out + and sum() differ in behaviour. Consider:
> sum(1:10,1:10)
[1] 110
> `+`(1:10,1:10)
[1]  2  4  6  8 10 12 14 16 18 20
library(dplyr)
df <- data_frame(w = letters[1:3], x=1:3, y = x^2, z = y - x)

#     Source: local data frame [3 x 4]
#     
#       w x y z
#     1 a 1 1 0
#     2 b 2 4 2
#     3 c 3 9 6


df %>% rowwise() %>% mutate(result = sum(x, y, z))

#   Source: local data frame [3 x 5]
#   Groups: <by row>
#     
#     w x y z result
#   1 a 1 1 0      2
#   2 b 2 4 2      8
#   3 c 3 9 6     18
df %>% mutate(result = x + y + z)
#   Source: local data frame [3 x 5]
#   
#     w x y z result
#   1 a 1 1 0      2
#   2 b 2 4 2      8
#   3 c 3 9 6     18
df %>% mutate(result = sum(x, y, z))  # sums over all of x, y and z and recycles the result!
#   Source: local data frame [3 x 5]
#   
#     w x y z result
#   1 a 1 1 0     28
#   2 b 2 4 2     28
#   3 c 3 9 6     28

Can you make dplyr::mutate and dplyr::lag default = its own input value?


Tag : r , By : Mare Astra
Date : March 29 2020, 07:55 AM
hop of those help? This is similar to this dplyr lag post, and this dplyr mutate lag post, but neither of those ask this question about defaulting to the input value. I am using dplyr to mutate a new field that's a lagged offset of another field (that I've converted to POSIXct). The goal is, for a given ip, I'd like to know some summary statistics on the delta between all the times it shows up on my list. I also have about 12 million rows. , In the OP's code ...
...
d) group_by(ip) %>%
e) mutate(shifted = dplyr::lag(fulldate, default=fulldate)) %>%
...

Mutate with a list column function in dplyr


Tag : r , By : Hans-Inge
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I am trying to calculate the Jaccard similarity between a source vector and comparison vectors in a tibble. , You could simply add rowwise
df_comp_jaccard <- df_comp %>%
  rowwise() %>%
  dplyr::mutate(jaccard_sim = length(intersect(names_vec, source_vec))/
                              length(union(names_vec, source_vec)))

# A tibble: 3 x 3
  names_ names_vec jaccard_sim
   <chr>    <list>       <dbl>
1  b d f <chr [3]>         0.2
2  u k g <chr [3]>         0.0
3  m o c <chr [3]>         0.2
tibble(a=1:10,b=10:1) %>% mutate(X = paste(a,b,sep="_"))
tibble(a=1:10,b=10:1) %>% rowwise %>% mutate(X = paste(a,b,sep="_"))
# # A tibble: 5 x 3
#       a     b     X
#   <int> <int> <chr>
# 1     1     5   1_5
# 2     2     4   2_4
# 3     3     3   3_3
# 4     4     2   4_2
# 5     5     1   5_1
tibble(a=1:5,b=5:1) %>% mutate(max(a,b))
# # A tibble: 5 x 3
#       a     b `max(a, b)`
#   <int> <int>       <int>
# 1     1     5           5
# 2     2     4           5
# 3     3     3           5
# 4     4     2           5
# 5     5     1           5

tibble(a=1:5,b=5:1) %>% rowwise %>% mutate(max(a,b))
# # A tibble: 5 x 3
#       a     b `max(a, b)`
#   <int> <int>       <int>
# 1     1     5           5
# 2     2     4           4
# 3     3     3           3
# 4     4     2           4
# 5     5     1           5
tibble(a=1:5,b=5:1) %>% mutate(pmax(a,b))
# # A tibble: 5 x 3
#       a     b `pmax(a, b)`
#   <int> <int>        <int>
# 1     1     5            5
# 2     2     4            4
# 3     3     3            3
# 4     4     2            4
# 5     5     1            5

When I don't know column names in data.frame, when I use dplyr mutate function


Tag : r , By : Juan Pablo
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I like to know how I can use dplyr mutate function when I don't know column names. Here is my example code; , With apply:
library(dplyr)
library(purrr)

df %>%
  mutate(minimum = apply(df[,2:4], 1, min))
df %>%
  mutate(minimum = pmap(.[2:4], min))
df %>%
  purrrlyr::by_row(~min(.[2:4]), .collate = "rows", .to = "minimum")
# tibble [3 x 5]
      w     x     y     z minimum
  <dbl> <dbl> <dbl> <dbl>   <dbl>
1     2     1     1     3       1
2     3     2     5     2       2
3     4     7     4     6       4
Related Posts Related QUESTIONS :
  • Create a filled area line plot with plotly
  • When do I need parentheses around an if statement to control the sequence of a formula in R?
  • my graph in ggplot2 contains an "e" character in y-axis
  • Making variables immutable in R
  • R: Difference between the subsequent ranks of a item group by date
  • Match data within multiple time-frames with dplyr
  • Conditional manipulation and extension of rows in data.table also considering previous extensions without for-loop
  • Conditional formula referring to preview row in DF not working
  • Set hoverinfo text in plotly scatterplot
  • Histogram of Sums from Categorical/Binary Data
  • Efficiently find set differences and generate random sample
  • Find closest points from data set B to point in data set A, using lat long in R
  • dplyr join on column A OR column B
  • Replace all string if row starts with (within a column)
  • Is there a possibility to combine position_stack and nudge_x in a stacked bar chart in ggplot2?
  • How can I extract bounding boxes in a row-wise manner using R?
  • How do I easily sum up values in different columns?
  • Reading numeric Date value from CSV file to data.frame in "R"
  • R programming: creating a stacked bar graph, with variable colors for each stacked bar
  • How to identify all columns that contain binary representation
  • Filter different groups by different factor levels
  • Saving .xlsx file to disc, form http post request
  • Add an "all" option under the filter that selects the number of rows displayed in a datatable
  • How to select second column of every xts in list
  • Generate a frequency dataframe out of an input dataframe
  • Why manual autocorrelation does not match acf() results?
  • Merge 3 dataframes which are different to each other
  • remove adjacent duplicates from string
  • How to change the position of stacked stacked bar chart in ggplot in R?
  • How to divide each of a range a variables by a second range of variables in R
  • Why do I need to assemble vector before scaling in Spark?
  • How to select individuals which appear in multiple groups?
  • How can I fill columns based on values in another column?
  • 32 bit R and 64 bit R: output differs
  • Remove a single backslash in paste0 output
  • ggplot2 different label for the first break
  • TSP in R, with given distances
  • How to find the given value from the range of values?
  • Solution on R group by issue _ multiple combination
  • Transform multiple columns with a function that uses different arguments per column
  • How can I parse a string with the format "1/16/2019 1:24:51" into a POSIXct or other date variable?
  • How to plot a box plot in R for outlier detection for a huge number of rows?
  • How to change column name according to another dataframe in R?
  • `sjPlot::tab_df()`--how to set the number of decimal places?
  • time average for specific time range in r
  • joining dataframes by closest time and another key in r
  • How to create nested for loop for a certain range
  • New category based on sequence of date ranges
  • how to extract formula from coxph model summary in R?
  • add row based on variable condition in R
  • Generating the sequence 111122222333334
  • Unable to use has_goog_key() in R
  • how to multiply each row with a scaler in corresponding column?
  • R is not recognizing levels of a factor as the same. Is there a way to do this?
  • Calculating mean of replicate experiment result values in a column based on multiple columns using R
  • Best method to extract the first instance of a string between specified keywords using data.table
  • ignore optional combination of alphanumeric characters in str_extract
  • Why tracemem shows two copies when modification occurs inside function body?
  • Can't use mppm on multitype point patterns
  • How to move selected matrix rows to top of matrix based on a selection vector of row names
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com