Disable scientific notation in t_test of rstatix package - r

I want to use to following exampe to do t-tests with multiple variables - The code is used from https://www.datanovia.com/en/blog/how-to-perform-multiple-t-test-in-r-for-different-variables/:
options(scipen = 99)
# Load required R packages
library(tidyverse)
library(rstatix)
library(ggpubr)
# Prepare the data and inspect a random sample of the data
mydata <- iris %>%
filter(Species != "setosa") %>%
as_tibble()
mydata %>% sample_n(6)
# Transform the data into long format
# Put all variables in the same column except `Species`, the grouping variable
mydata.long <- mydata %>%
pivot_longer(-Species, names_to = "variables", values_to = "value")
mydata.long %>% sample_n(6)
stat.test <- mydata.long %>%
group_by(variables) %>%
t_test(value ~ Species) %>%
adjust_pvalue(method = "BH") %>%
add_significance()
stat.test
This tutorial uses the t_test function of the rstatix package. It works great, but is there a way to disable the scientific notation of the p-values? I want to output p-values like 0.000445 instead of 4.45e-4.
Unfortunetely the use of
options(scipen = 99)
did not change anything.
Thank you!
EDIT: The solution can be found in the comments - it is necessary to call stat.test this way:
as.data.frame(stat.test)
Thank rawr for his comment!

Related

How to plot a bar graph in R using ggplot

I'm trying to plot this graph using R but dont know how.
Screenshot of the dataset that I'm using
You didn't provide the dataset, but you could do something like that
# packages ----------------------------------------------------------------
library(dplyr)
library(ggplot2)
# data --------------------------------------------------------------------
#load your data first
#
#
#get just the last observation for each location
dataset2 <- dataset %>%
group_by(location) %>%
slice_tail() %>%
ungroup()
# plot --------------------------------------------------------------------
dataset2 %>%
ggplot(aes(x = continent, y = total_deaths))+
geom_bar()

How to remove one row of column labels in a gt table?

#Preparing the data and loading packages
library(modelsummary);library(tidyverse);library(gt)
as_tibble(mtcars)
df <- mtcars %>% mutate(cyl_ = factor(cyl)) %>%
dplyr::select(cyl_, mpg, vs, am, hp, wt)
#Gets table of descriptive statistics about different subsets of the data
print(t1 <- datasummary_balance(~cyl_,
data = df,
output = "gt"))
#This hides the "Std. Dev." columns
t1 %>% cols_hide(c(3,5,7))
#Now I want to hide the "Mean" column labels, but I want to keep the "cyl_" value column labels. Any ideas how?
I want something like this:
Using the gt package, you can pipe your table to tab_options(column_labels.hidden = TRUE) to remove column labels. Unfortunately, this will remove both levels: the column headers, and the spanning labels that include the cyl info you want to keep.
Note that datasummary_balance() produces a highly customized table which is intended to be used as a ready-made output. In cases like these, it might be easier to just build the custom table you want using datasummary() instead of trying to customize datasummary_balance() (square peg, round hole, etc). For example:
library(modelsummary)
library(tidyverse)
df <- mtcars %>%
select(cyl, mpg, vs, am, hp, wt) %>%
mutate(cyl = factor(sprintf("%s (N = %s)", cyl, n()))) %>%
as.data.frame() # The `All()` function does not accept tibbles
datasummary(
All(df) ~ Mean * cyl,
data = df,
output = "gt")

How do I debug or resolve a 'corrupt x' error when using R map command?

I'm trying to recreate the example in Hadley's R For Data Science book (https://r4ds.had.co.nz/many-models.html). I can get the first part to work, but when I try to run the map command, I get Error: Corrupt x: no names.
Here's the code
install.packages("gapminder")
library(gapminder)
gapminder
gapminder %>%
ggplot(aes(year, lifeExp, group = country)) +
geom_line(alpha = 1/3)
by_country <- gapminder %>%
group_by(country, continent) %>%
nest()
by_country
by_country$data[[1]]
country_model <- function(df) {
lm(lifeExp ~ year, data = df)
}
models <- map(by_country$data, country_model)
Here are all the packages I added
library(tidyverse);
library(lubridate)
library(stringr)
library(mapdata)
library(maps)
library(viridis)
library(modelr)
I was trying to do something similar and kept getting this error, so I decided to exactly what Hadley did and got the same error. What am I missing?

Plot multiple histograms with purrr::map and ggplot2

How do I plot the histograms for these four random variables . This works, but it seems unneccessarily long.
#libraries
library(tidyverse)
library(purrr)
# Standard deviation question
std_devs %>%
map(rnorm, n=1000, mean=75) %>%
do.call('rbind', .) %>%
t() %>%
as.data.frame() %>%
gather() %>%
ggplot(., aes(x=value))+geom_histogram()+facet_wrap(~key)
purrr is loaded by tidyverse so you can skip that line. map_df makes the rest much more condensed.
library(tidyverse)
# Standard deviation question
set.seed(10)
std_devs <- 1:4
std_devs %>%
map_df(~data_frame(key = ., value = rnorm(n=1000, mean=75, sd = .))) %>%
ggplot(aes(x=value))+geom_histogram()+facet_wrap(~key)

creating plot side effects with do or summarise in dplyr

I'm using do() to fit a model to grouped data, and then I want to plot the fit for each group. In plyr, I guess I would use d_ply(). In dplyr, I'm trying either do() or summarise() using a function that makes the plot as a side effect.
I'm getting different results depending on whether I use do() or summarise(), and I'm not sure why. Specifically it seems like summarise() isn't operating on each row correctly.
Here's my example:
require(nycflights13)
require(mgcv)
# fit a gam to the flights grouped by dest (from ?do)
by_dest <- flights %>% group_by(dest) %>% filter(n() > 100)
models = by_dest %>% do(smooth = gam(arr_delay ~ s(dep_time) + month, data = .))
# print the first 4 rows, the dest is ABQ, ACK, ALB, ATL
models %>% slice(1:4)
# make a function to plot the models, titled by dest
plot.w.title = function(title, gam.model){
plot.gam(gam.model, main=title)
return(1)
}
# This code makes plots with the wrong titles, for example ATL is listed twice:
models %>%
slice(1:4) %>%
rowwise %>%
summarise(useless.column = plot.w.title(dest, smooth)) # for plot side effect
# this code gives me the correct titles...why the difference?
models %>%
slice(1:4) %>%
rowwise %>%
do(useless.column = plot.w.title(.$dest, .$smooth))
The summarise() method will work if you modify the function by applying unique() to the title:
plot.w.title = function(title, gam.model){
plot.gam(gam.model, main=unique(title))
return(1)
}

Resources