When I run the following line by line in R it works fine, but with knitr the options(digits=3) gets ignored.
Why? Any solutions?
<<cor>>=
#mock up data set.
x <- c(rnorm(100))
y <- c(rnorm(100))
z <- c(rnorm(100))
df <- as.data.frame(cbind(x,y,z))
df$x<- as.numeric(df$x)
df$y<- as.numeric(df$y)
df$z<- as.numeric(df$z)
options(digits=3)
cor(df, use = 'na.or.complete', method = c("spearman"))
#
I found a solution while searching for a different issue. Leave out options(digits=3) and use
round(cor(df, use = "na.or.complete", method = c("spearman")), digits = 3)
Which leaves the question why the options(...) doesn't work. But I can happily live with that!
Thanks everyone for their time!
Gerit
Related
I have some code that loops over a list of study IDs (ids) and turns them into separate polygons/spatial points. On the first execution of the loop it produces the following error:
Error in (function (x) : attempt to apply non-function
This is from the raster::rasterToPoints function. I've looked at the examples in the help section for this function and passing fun=NULL seems to be an acceptable method (filters out all NA values). All the values are equal to 1 anyways so I tried passing a simple function like it suggests such as function(x){x==1}. When this didn't work, I also tried to just suppress the error message but without any luck using try() or tryCatch().
Main questions:
1. Why does this produce an error at all?
2. Why does it only display the error on the first run through the loop?
Reproducible example:
library(ggplot2)
library(raster)
library(sf)
library(dplyr)
pacific <- map_data("world2")
pac_mod <- pacific
coordinates(pac_mod) <- ~long+lat
proj4string(pac_mod) <- CRS("+init=epsg:4326")
pac_mod2 <- spTransform(pac_mod, CRS("+init=epsg:4326"))
pac_rast <- raster(pac_mod2, resolution=0.5)
values(pac_rast) <- 1
all_diet_density_samples <- data.frame(
lat_min = c(35, 35),
lat_max = c(65, 65),
lon_min = c(140, 180),
lon_max = c(180, 235),
sample_replicates = c(38, 278),
id= c(1,2)
)
ids <- all_diet_density_samples$id
for (idnum in ids){
poly1 = all_diet_density_samples[idnum,]
pol = st_sfc(st_polygon(list(cbind(c(poly1$lon_min, poly1$lon_min, poly1$lon_max, poly1$lon_max, poly1$lon_min), c(poly1$lat_min, poly1$lat_max, poly1$lat_max, poly1$lat_min, poly1$lat_min)))))
pol_sf = st_as_sf(pol)
x <- rasterize(pol_sf, pac_rast)
df1 <- raster::rasterToPoints(x, fun=NULL, spatial=FALSE) #ERROR HERE
df2 <- as.data.frame(df1)
density_poly <- all_diet_density_samples %>% filter(id == idnum) %>% pull(sample_replicates)
df2$density <- density_poly
write.csv(df2, paste0("pol_", idnum, ".csv"))
}
Any help would be greatly appreciated!
These are error messages, but not errors in the strict sense as the script continues to run, and the results are not affected. They are related to garbage collection (removal from memory of objects that are no longer in use) and this makes it tricky to pinpoint what causes it (below you can see a slightly modified example that suggests another culprit), and why it does not always happen at the same spot.
Edit (Oct 2022)
These annoying messages
Error in x$.self$finalize() : attempt to apply non-function
Error in (function (x) : attempt to apply non-function
Will disappear with the next release of Rcpp, which is planned for Jan 2023. You can also install the development version of Rcpp like this:
install.packages("Rcpp", repos="https://rcppcore.github.io/drat")
Code that use to run:
library(caret)
library(tidyverse)
data_set <- diamonds %>%
filter(row_number() <= 1000) %>%
select(carat, depth)
featurePlot(x = data_set[,-1], y = data_set[,1])
now fails with
Error in diff(as.numeric(y[ord])) :
(list) object cannot be coerced to type 'double'
Below also fails:
featurePlot(x = as.data.frame(data_set[,-1]), y = as.data.frame(data_set[,1]))
But below works:
featurePlot(x = data_set$depth, y = data_set$carat)
Seeking a way to use sub setting by column number.
I think there is something going on with the tibble class---I've encountered something similar. Try the following. It works for me:
class(data_set) <- "data.frame"
featurePlot(x = data_set[,-1], y = data_set[,1])
You might want to file an issue on GitHub, because it is indeed strange that your second approach (which is very sensible) does not work. I can confirm that both approaches that you tried also generate the same errors for me.
I am looking at this link.
https://bioconductor.org/packages/devel/bioc/vignettes/ComplexHeatmap/inst/doc/s2.single_heatmap.html
This first several lines of code work fine, but now I'm getting this error.
Error in Heatmap(mat) : could not find function "Heatmap"
I'm no too familiar with R. Can someone tell me what is wrong here? Thanks.
Try running the following code:
source("https://bioconductor.org/biocLite.R")
if(!require(ComplexHeatmap)) biocLite("ComplexHeatmap")
if(!require(circlize)) install.packages('circlize')
# -------------------------------------------------------
library(ComplexHeatmap)
library(circlize)
set.seed(123)
mat = cbind(rbind(matrix(rnorm(16, -1), 4), matrix(rnorm(32, 1), 8)),
rbind(matrix(rnorm(24, 1), 4), matrix(rnorm(48, -1), 8)))
# permute the rows and columns
mat = mat[sample(nrow(mat), nrow(mat)), sample(ncol(mat), ncol(mat))]
rownames(mat) = paste0("R", 1:12)
colnames(mat) = paste0("C", 1:10)
Heatmap(mat)
Does this work for you?
If yes, your problem was that you had not installed the necessary packages before loading them with library. The first 3 lines of the code I provided is simply checking if you have installed the necessary packages, and then if not, it installs them.
I am attempting to run the corr.test equation in R, with code that my professor submitted and tested on his system. Unfortunately, when I run it I am getting an error that "object sef not found".
This is confounding both my professor and I, and having done a thorough search, we're not sure how to address this.
I really appreciate any help you can provide.
Edit: Here is the code I am using:
trendan1 <- read.table("trendan1.for.R.dat", header=TRUE, na.strings=".")
head(trendan1)
tail(trendan1)
attributes(trendan1)
is.matrix(trendan1)
id <- trendan1$id
famenv1 <- trendan1$famenv1
famenv2 <- trendan1$famenv2
famenv3 <- trendan1$famenv3
conf1 <- trendan1$conf1
conf2 <- trendan1$conf2
conf3 <- trendan1$conf3
trendan1dataset1 <- cbind(id,famenv1,famenv2,famenv3,conf1,conf2,conf3)
attributes(trendan1dataset1)
is.matrix(trendan1dataset1)
is.data.frame(trendan1dataset1)
require("psych")
describe(trendan1dataset1[,2:7])
print(describe(trendan1dataset1[,2:7]), digits=6)
famave <- (1*famenv1 + 1*famenv2 + 1*famenv3)/3
famlin <- -1*famenv1 + 0*famenv2 + 1*famenv3
famquad <- 1*famenv1 - 2*famenv2 + 1*famenv3;
trendandataset2 <- cbind(famenv1,famenv2,famenv3,famave,famlin,famquad)
print(describe(trendandataset2), digits=6)
hist(famenv1)
boxplot(famenv1)
abline(h=mean(famenv1))
qqnorm(famenv1,ylab="famenv1")
qqline(famenv1)
shapiro.test(famenv1)
hist(famenv2)
boxplot(famenv2)
abline(h=mean(famenv2)) # add mean to the boxplot
qqnorm(famenv1,ylab="famenv2")
qqline(famenv2)
shapiro.test(famenv2)
corvars1 <- cbind(famenv1,famenv2,famenv3)
cor(corvars1,use = "everything", method = "pearson")
cov(corvars1,use = "everything")
sscp1 <- t(corvars1)%*%(corvars1) #Matrix multiplcation
sscp1
rc1 <- corr.test(corvars1,
use="pairwise",method="pearson",adjust="holm",alpha=.05, ci=FALSE)
attributes(rc1)
print(rc1$p, digits=6)
This is a bug that sometimes happens when you do not evaluate confidence interval. It should be fixed if u change the option to ci=TRUE, or simply delete this option as the default is ci=TRUE.
I have a problem with sjPlot command: sjt.grpmean
This is my example:
x <- rnorm(120,1230,220)
f <- c(rep("men",60),rep("women",60))
d <- data.frame(x,f)
library(sjPlot)
sjt.grpmean(var.cnt = d$x,
var.grp = d$f)
The result is just weird.
I try this:
x <- rnorm(120,1230,220)
f <- as.factor(c(rep("men",60),rep("women",60)))
d <- data.frame(x,f)
library(sjPlot)
sjt.grpmean(var.cnt = d$x,d$f)
With same weird result.
I Can use this workaround:
sjt.grpmean(var.cnt = d$x,
var.grp = d$f,
value.labels = c("men","women"))
But, why i need to use value.labels to fix?
is this a bug?
I will appreciate any help.
R version 3.3.2 (2016-10-31)
sjPlot 2.3.0
is this a bug?
Yes, it's a bug you found. I have already fixed it in the current dev-version on GitHub, a CRAN-update may follow in March...