37,612 questions
0
votes
1
answer
113
views
How to get rid of “vec_is_vector() is defunct” error in mutate() function?
How do I get the mutate function, from the dplyr package, to work when R says I have input problems due to vec_is_vector being defunct?
I’ve recently updated R and noticed that some of my old code no ...
0
votes
2
answers
153
views
Use selectors in replace_na
Consider the following example:
df<-data.frame(x_1=c(NA,1,NA),
x_2=c(1,1,NA),
y=c(NA,1,1))
What's the minimum number of code characters that allows me to use ...
4
votes
1
answer
254
views
Combination of dplyr and data.table silently breaks table structure
Consider the following code:
require(dplyr)
require(data.table)
dt_original <- data.table(
colA_char = c("A1", "A2", "A3"),
colB_char = c("B1", "...
Best practices
2
votes
3
replies
126
views
Use !! or {{ }} to create new columns in functions using dplyr
I haven't used meta-programming operators in R for a while, and I came across this today. What is the recommended way to add a new column?
# Using embrace operator
add_col1 <- function(data, ...
2
votes
3
answers
133
views
Using group_by with user defined function [duplicate]
I've made a function to calculate the percentage of students who progressed to HE. Now I want to filter and group my data in different ways and calculate the percentage of each group that progressed ...
4
votes
3
answers
184
views
How to mutate new variable according the formula string
How to mutate new variable according the string my_formula1 and my_formula2 ?
library(tidyverse)
# sample 1 : this can work ,by the new variable not 'total' ( it's is `eval(str2lang(my_f))`)
...
6
votes
7
answers
541
views
there are two case_when() in code,the conditions are the same. Is the any way to simplify it
in below code ,there are two case_when(),the conditions are the same. Is the any way to simplify it ?
library(tidyverse)
diamonds %>%
mutate(kpi_a = case_when(
color %in% c('E','I') ~ "A&...
7
votes
2
answers
245
views
case_when() unexpected behavior
I was trying the case_when() call in R and realized an interesting behavior. A simplified example:
Expected behavior
> case_when(0 < 1 ~ "a",
2 > 3 ~ "b",
...
Best practices
1
vote
1
replies
69
views
Visualizing percentages values in Euler diagrams in R
I will use Euler diagrams (from the eulerr package) to visualize A, B, and the intersections of A and B. My data points are A: 21%, B: 24%, and “A&B”: 14%, and my goal is to visualize all three ...
1
vote
2
answers
120
views
Remove rows conditionally if one column does not include a set of required strings within groups defined by another column [duplicate]
I have data that looks like this:
individual <- 1:10
group <- c("A", "A", "B", "B", "B", "C", "C", "D", "D&...
3
votes
3
answers
214
views
Create named groups by dates using defined time window in R
I want to create a column that labels each row as belonging to a group based on the date in one of the other columns. I want to be able to set the time window that define groupings, eg within 1 week ...
1
vote
7
answers
315
views
rearrange some columns in R alphabetically [duplicate]
I have a dataframe with >100 variables, and I want to rearrange only some of the variables alphabetically whilst leaving the rest where they are
I know there are MANY posts that answer how to ...
Best practices
0
votes
3
replies
80
views
How to efficiently extract variables from a python function into an r dataframe using dplyr?
I have a dataframe called Profile_1_2 of data that is formatted like this:
Profile_number
chl_z
Cphyto_z
1
0.2
10
1
0.6
100
2
0.1
20
2
0.5
90
Profile_number denotes a group, each profile contains 200 ...
3
votes
1
answer
150
views
Replacing several rows of data in a column efficiently using condition in a pipeline
I have the following dataframe:
df <- data.frame(
Form=rep(c("Fast", "Medium", "Slow"), each = 3),
Parameter =rep(c("Fmax", "TMAX", "B&...
5
votes
2
answers
195
views
How to conditionally pass a vector to dplyr::select using quosures?
I am trying to set up a conditional argument in a function to drop columns from the tibble based on column name. There are a large number of columns but many are adjacent to one another and I would ...