Here is my code:
library(tidyr)
messy <- data.frame(
name = c("Wilbur", "Petunia", "Gregory"),
a = c(67, 80, 64),
b = c(56, 90, 50)
)
And I would like to use gather function with variable/function result. Borrowing from I tried:
not_messy <-messy %>%
gather_('drug', 'heartrate', paste0('a',''):paste0('b',''))
But it generated error:
Error in paste0("a", ""):paste0("b", "") : NA/NaN argument
In addition: Warning messages:
1: In lapply(.x, .f, ...) : NAs introduced by coercion
2: In lapply(.x, .f, ...) : NAs introduced by coercion
What am I missing?
With the latest version of the tidyverse functions, you are discouraged from using the underscore versions of the function for standard evaluation and instead use the rlang function syntax. In this case you can use
gather(messy, "drug", "heartrate", (!!as.name("a")):(!!as.name("b")))
Related
I am building an R library for Bioconductor, and one of the lines in the code seems to be problematic only when calling it from another function in RStudio (i.e. when doing the lines one by one it seems to work, and when using R in terminal it also seems to work).
This is the problematic function:
get_cres_tiled_genome <- function(cres, assembly="hg19", chr="chr22", binsize=5e3){
bins <- tileGenome(seqinfo(load_bs_genome(assembly))[chr], tilewidth=binsize, cut.last.tile.in.chrom=TRUE);bins <- bins[width(bins) == binsize];
bins <- keepStandardChromosomes(bins, pruning.mode="coarse");seqlevelsStyle(bins) <- "UCSC"
keep.bins.idx <- 1:length(bins)
cres$bin <- Rle(floor((end(cres)-1)/binsize)+1)
cres <- cres[cres$bin %in% keep.bins.idx]
binned.cres <- bins[unique(cres$bin)]
return(binned.cres)
}
Anyway this is the error:
Error in h(simpleError(msg, call)) : error in evaluating the
argument 'i' in selecting a method for function '[': 'match' requires
vector arguments
5. h(simpleError(msg, call))
4. .handleSimpleError(function (cond) .Internal(C_tryCatchHelper(addr, 1L, cond)), "'match' requires vector
arguments",
base::quote(match(x, table, nomatch = 0L)))
3. cres$bin %in% keep.bins.idx
2. cres[cres$bin %in% keep.bins.idx] at generate_candidates.R#116
get_cres_tiled_genome(cres = cres, assembly = assembly, chr = chr,
binsize = binsize)
This leads me to suppose that the error is coming from cres[cres$bin %in% keep.bins.idx]. The question is, could there be another way to write this so that this error doesn't pop up? I don't seem to quite understand where the problem comes from, given the situation described at the start of the question.
I'm a Python guy who is asked to run some R code that returns the following error:
Error: ... is not empty.
We detected these problematic arguments:
..1
These dots only exist to allow future extensions and should be empty.
Did you misspecify an argument? Run rlang::last_error() to see where
the error occurred.
I could reduce the code to this MWE:
library(dplyr)
x <- data.frame(1)
x %>% ungroup(x)
I have no idea what line 3 is supposed to do, but it fails on my system (dplyr 1.0.0), while working using dplyr 0.8.5 or on https://rdrr.io/snippets/, where it prints
X1
1 1
I have tried a number of things, with no success:
update.packages(ask = FALSE)
remove.packages("dplyr")
install.packages("dplyr")
What is going on here? How can I (help) investigate?
Update: options(error = recover) gives me this:
1: x %>% ungroup(x)
2: withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
3: eval(quote(`_fseq`(`_lhs`)), env, env)
4: eval(quote(`_fseq`(`_lhs`)), env, env)
5: `_fseq`(`_lhs`)
6: freduce(value, `_function_list`)
7: withVisible(function_list[[k]](value))
8: function_list[[k]](value)
9: ungroup(., x)
10: ungroup.data.frame(., x)
11: ellipsis::check_dots_empty()
12: action_dots(action = action, message = "`...` is not empty.", dot_names = n
13: action(message, .subclass = c(.subclass, "rlib_error_dots"), ...)
14: signal_abort(cnd)
Another update: the complete line of code, non-minimized, if that matters, is
screenData <- mutate_if(screenData, is.character, as.factor) %>% ungroup(screenData)
Maybe that makes more sense than my MWE.
Another one: dput(screenData) returns
structure(list(wellID = "A001", rowID = "A0", colID = "01", value = 0,
fileName = "V3_Prob5_p1", batch = structure(NA_integer_, .Label = character(0), class = "factor"),
sampleID = NA, patientID = NA, name = NA_character_, concentration = NA_real_,
wellType = "sample"), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame"))
Finally, I opened an issue with the maintainer of the code, see https://github.com/lujunyan1118/DrugScreenExplorer/issues/1,
and with dplyr, see https://github.com/tidyverse/dplyr/issues/5368
The pipe %>% is syntactic sugar. It means "take the object on the left hand side of the pipe and use it as the first argument to the function on the right-hand side of the pipe".
So you can say ungroup(x) or x %>% ungroup(), both of which work, but x %>% ungroup(x) is trying to both ungroup a data.frame called x and remove a variable called x from the grouping of the data.frame called x. But the data.frame x doesn't contain a variable called x. Hence the problem.
The code you've been given is inherently wrong (or at the very least confusingly written).
Update
Following the post of your dput.
screenData %>% ungroup(screenData) is equivalent to ungroup(screenData, screenData) where the first screenData is the data.frame to ungroup and the second is the name of the variable in the data.frame to remove from the grouping. but the data.frame screenData does not contain a column called screenData. That's why you get an error.
The code you have been given is unequivocally incorrect.
The fact that it ran without error in a previous version of dplyr is purely accidental.
The ungroup function takes no extra arguments. The code you’ve been provided with is wrong. Remove x from the ungroup call.
It seems an issue with tibble package. See here.
Updating tibble solved the issue for me.
I'm trying to replace the values which I've set by default in a data frame by the calculated ones but I get an error that I don't understand as far as I've no factors.
Here is the code :
nb_agences_iris <- agences %>%
group_by(CODE_IRIS) %>%
summarise(nb_agences = n()) %>%
arrange(CODE_IRIS)
int <- data.frame("CODE_IRIS" = as.character(intersect(typo$X0, nb_agences_iris$CODE_IRIS)))
typo$nb_agences <- as.character(rep(0, nrow(typo)))
typo[int$CODE_IRIS,]$nb_agences <- as.character(nb_agences_iris[int$CODE_IRIS,]$nb_agences)
And I get the following error:
Error in Summary.factor(1:734, na.rm = FALSE) :
‘max’ not meaningful for factors
In addition: Warning message:
In Ops.factor(i, 0L) : ‘>=’ not meaningful for factors
Thanks in advance for your help.
I am hoping someone can help determine why I receive an error message when using lazy evaluation as part of a dplyr filter_ verb. The end goal is to pass arguments by reference using a function but I have narrowed the problem down outside of a function.
library(dplyr)
library(lazyeval)
library(data.table)
data_raw_dt <- data.table(
R_dates = seq(from = as.Date("2015/8/31"), by = "1 day", length.out = 30),
grp_region = sample(letters[1:4], 30, replace = TRUE),
z_valuation = rnorm(30)
)
# Create some NAs
data_raw_dt$grp_region[data_raw_dt$grp_region == "d"] <- NA
dates = "R_dates"
group = "grp_region"
column = "z_valuation"
filter_criteria = interp(~(!is.na(var)), var = as.name(group))
data_raw_dt %>%
filter_(filter_criteria)
But this gives the following error message:
"Error in lazyeval::common_env(.dots) :
argument ".dots" is missing, with no default"
In this case, I am not sure how to specify .dots and when I do it asks for a list. I have checked here, here, and here and structured my code following these examples with no success.
Package version:
dplyr 0.4.2
lazyeval 0.1.10
data table 1.9.4
Does anyone have any ideas? Thank you so much in advance!
Just replace with
filter_(.dots = filter_criteria)
I am writing an R package where only a small subset of the functions use functions from data.table. Following Wickham's advice, I added data.table in the Suggests: field of the DESCRIPTION file. I also added if(! requireNamespace("data.table", quietly=TRUE)) at the beginning of each of my functions using a function from data.table. Moeover, each time, I use a data.table-specific function, I precede it with data.table::.
However, I am still encountering problems. As the FAQ of data.table only deals with the Depends: and Imports: fields of the DESCRIPTION file, does it mean that Suggests isn't an option?
Here is a function causing problems:
depths.per.sample <- function(dat, min.reg.len=30, max.reg.len=500,
min.reg.dep=10, max.reg.dep=100,
min.reg.frac=0.25){
if(! requireNamespace("data.table", quietly=TRUE))
stop("Pkg needed for this function to work. Please install it.",
call.=FALSE)
stopifnot(data.table::is.data.table(dat))
for(col in c("ind", "flowcell", "lane", "start", "end", "depth", "fraction"))
stopifnot(col %in% colnames(dat))
## http://stackoverflow.com/a/8096882/597069
depth=fraction=chrom=ind=flowcell=lane=NULL
data.table::setkey(dat, NULL)
data.table::setkeyv(x=dat, cols=c("ind", "flowcell", "lane"))
depths.sample <- dat[end - start >= min.reg.len &
end - start <= max.reg.len,
list(depth.len=data.table::.N,
depth.min=min(data.table::.SD[,depth]),
depth.med=as.double(median(data.table::.SD[,depth])),
depth.mean=mean(data.table::.SD[,depth]),
depth.max=max(data.table::.SD[,depth]),
depth.q65=quantile(data.table::.SD[,depth], 0.65),
depth.q70=quantile(data.table::.SD[,depth], 0.70),
depth.q75=quantile(data.table::.SD[,depth], 0.75),
depth.q80=quantile(data.table::.SD[,depth], 0.80),
reg.ok=nrow(unique(data.table::.SD[depth >= min.reg.dep &
depth <= max.reg.dep &
fraction >= min.reg.frac,
list(chrom,start,end)]))),
by=list(ind,flowcell,lane)]
return(depths.sample)
}
And here are the errors:
Error in x[j] : invalid subscript type 'list'
In addition: Warning messages:
1: In min(data.table::.SD[, depth]) :
no non-missing arguments to min; returning Inf
2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
3: In mean.default(data.table::.SD[, depth]) :
argument is not numeric or logical: returning NA
4: In max(data.table::.SD[, depth]) :
no non-missing arguments to max; returning -Inf