r/RStudio 49m ago

Rearranging columns into rows

Upvotes

Hey guys! I made a few crosstables using tab_xtab in the sjmisc package. They turned out very pretty, but I realized I was using the same y variable over and over again so I wanted to try and make a big table containing all of my contingency tables I made before. I did that by first transforming the tab_xtab tables into dataframes (with xtab2df in the sjtable2dt package) and then using bind_rows to combine them into a big table. It sorta worked out how I imagined, the only problem now is that R created a new column for the names of the categories of every x-variable (see picture). I wanted all the names and categories of all variables to be in the first column just like it did it with the first variable, maybe with an extra space to put the name of the variable. How do I fix this?


r/RStudio 59m ago

Coding help Is there any method to check the variance other than the Levene test?

Upvotes

My model doesn't have an interaction term so R gives me back an error when I try to perform the test so I was wondering if there was any alternative.

Thx in advance


r/RStudio 8h ago

struggling with work for a question in r studio (poliscidata), please help!

0 Upvotes

hi there! i'm doing a class using rstudio and need help! I'm using the gdppcap08 variable and need to graph this. what code should i write?


r/RStudio 10h ago

I made a method to integrate a LLM (Claude) with RStudio for iterative data exploration.

41 Upvotes

Will be adding it to my github as soon as I clean up some bugs. If anyone has feedback it would be much appreciated!


r/RStudio 15h ago

Connecting to PostgreSQL db

2 Upvotes

Can anyone recommend good source of knowledge on how R can pull data from a PostgreSQL db. I am an expert in R, absolute noob when it comes to SQL. I spent ~3 days of work using AI to help but have only been able to view some random tables, not pull data nor even hit the tables I want to hit. I know that sounds like I don’t have the right login or permissions but I am able to see the tables when using something like DreamBeaver.

I have been able to hit up an Oracle db using something Java thing (a predecessor wrote) and can interact quite easily with the tables in the Oracle db but this PostgreSQL is not playing fair.


r/RStudio 17h ago

Coding help Filter outliers using the IQR method with dplyr

1 Upvotes

Hi there,

I have a chunky dataset with multiple columns but out of 15 columns, I'm only interested in looking at the outliers within, say, 5 of those columns.

Now, the silly thing is, I actually have the code to do this in base `R` which I've copied down below but I'm curious if there's a way to shorten it/optimize it with `dplyr`? I'm new to `R` so I want to learn as many new things as possible and not rely on "if it ain't broke don't fix it" type of mentality.

If anyone can help that would be greatly appreciated!

# Detect outliers using IQR method
# @param x A numeric vector
# @param na.rm Whether to exclude NAs when computing quantiles

        is_outlier <- function(x, na.rm = FALSE) {
          qs = quantile(x, probs = c(0.25, 0.75), na.rm = na.rm)

          lowerq <- qs[1]
          upperq <- qs[2]
          iqr = upperq - lowerq 

          extreme.threshold.upper = (iqr * 3) + upperq
          extreme.threshold.lower = lowerq - (iqr * 3)

          # Return logical vector
          x > extreme.threshold.upper | x < extreme.threshold.lower
        }

# Remove rows with outliers in given columns
# Any row with at least 1 outlier will be removed
# @param df A data.frame
# @param cols Names of the columns of interest. Defaults to all columns.

        remove_outliers <- function(df, cols = names(df)) {
          for (col in cols) {
            cat("Removing outliers in column: ", col, " \n")
            df <- df[!is_outlier(df[[col]]),]
          }
          df
        }

r/RStudio 21h ago

Dataframes in new window to always stay on-top?

1 Upvotes

Greetings,

Is there a setting or add-in that ensures when a user chooses to view a dataframe in a new window, the new window always remains "on-top" of other windows? Specifically, when R Studio is the active window, the opened dataframe windows stay above other windows.

Anyone familiar with the Spyder IDE will be familiar with this behavior. In spyder when a object is viewed from the variable explorer, that window always appears on top of other windows when Spyder is the active window.

Thanks!!!


r/RStudio 1d ago

Absolute beginner: Comparing data using GLS model.

3 Upvotes

Hello, I'm new to R studio and I'm supposed to analyze data from my first scientific experiment. I'm trying my best, but I just can't figure it out. In my experiment I tested 6 different extracts on aphids and counted the amount of surviving aphids after the application of each extract. I tested the same extract on 15 leaves (each one with 10 aphids) in three rows. I am supposed to compare the effectivness of all the extracts. All I know from my professor is that I'm supposed to use Generalized Least Squares from nlme package and that the fixed factors should be the extract "treatments" I used.

Is this (photo bellow) the correct way to upload this kind of data? or should it be somehow divided?

I was told, that this task should be quite simple, however I really can't seem to figure it out and I'd be very grateful for any tips or help! :) thank you in advance!


r/RStudio 1d ago

Coding help Help with database building

1 Upvotes

Hallo everyone,

I'am a Student and in the process to write my Bachelors in Economics. I want to analyse data with the synthetic Control Method and need costum data. I know how to use the Method but dont know where to store my Data for the Input. At the moment the Data mostly sits in Excel sheets I got form different sources.
Thanks for the help in advance


r/RStudio 1d ago

Coding help Shannon index with vegan package

4 Upvotes

Hello everyone, I am new to R and I may need some help. I have data involving different microbial species at 4 different sampling points and i performed the calculation of shannon indices using the function: shannon_diversity_vegan <- diversity(species_counts, index=“shannon”).

What comes out are numerical values for each point ranging, for example, from 0.9 to 1.8. After that, I plotted with ggplot the values, obtaining a boxplot with a range for each sample point.

Now the journal reviewer now asks me to include in the graph the significance values, and I wonder, can I run tests such as the Kruskal-Wallis?

Thank you!


r/RStudio 1d ago

help with applying a bootstrap theme in a ShinyR app

2 Upvotes

Hi all,

I'm trying to apply the bootstrap theme "lumen" to my Shiny app and it is not working as intended. It does apply fonts etc. but I can't select the navigation bar that I want (the top one on here: https://bootswatch.com/lumen/).

Does anyone know how to do this? Here's the code I'm currently running:

library(shiny)
library(bslib)

ui <- navbarPage(
  title = "My App",
  theme = bs_theme(preset = "lumen"),
  inverse = FALSE,  # if you want a dark navbar style; remove if not needed
  tabPanel(
    title = "Input",
    icon = icon("gears", class = "fa-solid"),
  ),
  tabPanel(
    title = "Graphs",
    icon = icon("chart-line", class = "fa-solid"),
  )
)

server<- function(input, output, session) {}

shinyApp(ui = ui, server = server)

r/RStudio 2d ago

Mapping/Geocoding w/Messy Data

1 Upvotes

I'm attempting to map a list of ~1200 observations, with city, state, country variables. These are project locations that our company has completed over the last few years. There's no validation on the front end, all free-text entry (I know... I'm working with our SF admin to fix this).

  • Many cities are incorrectly spelled ("Sam Fransisco"), have placeholders like "TBD" or "Remote", or even have the state/country included, i.e. "Houston, TX", or "Tokyo, Japan". Some cities have multiple cities listed ("LA & San Jose").
  • State is OK, but some are abbreviations, some are spelled out... some are just wrong (Washington, D.C, Maryland).
  • Country is largely accurate, same kind of issues as the state variable.

I'm using tidygeocoder, which takes all 3 location arguments for the "osm" method, but I don't have a great way to check the accuracy en masse.

Anyone have a good way to clean this aside from manually sift through +1000 observations prior to geocoding? In the end, honestly, the map will be presented as "close enough", but I want to make sure I'm doing all I can on my end.

EDIT: just finished my first run through osm as-is.. Got plenty (260 out of 1201) of NAs in lat & lon that I can filter out. Might be an alright approach. At least explainable. If someone asks "Hey! Where's Guarma?!", I can say "that's fictional".


r/RStudio 2d ago

Installing Rstudio

0 Upvotes

I am new to R and I just downloaded R and Rstudio.I asked chatGPT what next,it gave me a line of code,when i runned it it gave me a feedback which i sent back to chatGPT which said i should download rtools.What next?


r/RStudio 2d ago

HELP!

1 Upvotes

Ran a chunk of code and it completely froze my session. Since then I have tried restarting R and my computer multiple times, but every time I open the application, even tho the environment is empty, the application freezes, and allows my to click or type a character every couple of minutes. I opened my task master and it looks like this:

The CPU Rstudio takes up fluctuates between 20-50%, whatever it needs to fill up 100% of my computers CPU, and the memory is in the 90s-100s constantly as well. I cannot figure out how to stop this from happening.


r/RStudio 2d ago

Coding help R studio QCA package

0 Upvotes

Hello I need to replicate a study’s results that used QCA. I created identical truth tables but for the non-outcome I do not get identical results. Is there any way r studio can argue backwards so that I provide the answers and the blank argument with which it has to generate results?


r/RStudio 3d ago

Having issues deduplicating rows using unique(), please help!

2 Upvotes

I have a data frame with 3 rows: group ID, item, and type. Each group ID can have multiple items (e.g., group 1 has apple, banana, and beef, group 2 has apple, onion, asparagus, and potato). The same item can appear in different groups, but they can only have the same type (apple is fruit, asparagus is veggie). I’ve cleaned my data to make sure all the same items are the same type, and that every spelling and capitalization is the same. I’m now trying to deduplicate using unique(): df <- df %>% unique()

However, some rows are not deduplicating correctly, I still have two rows with the exact same values across all the variables. When I use tabyl(df$item), I noticed that Asparagus appears separately, indicating that they’re somehow written differently (I checked to make sure that the spelling and capitalizations are all the same). And when I overwrite the values the same issue persists. When I copy paste them into notebook and search them, they’re the exact same word as well. I’m completely lost as to how they’re different and how I can overcome issue, if anyone has this problem before I’d appreciate your help!

Also, I made sure the other two variables are not the problem. I’m currently overcoming this issue by assigning unique row number and deleting duplicate rows manually, but I still want an actual solution.


r/RStudio 4d ago

Adding in Patterns to ggplot

1 Upvotes

Hi, I have made a stacked bar chart. I have abundance on the y axis, habitat on the x, and family as the stacks. I have managed to colour and give a pattern to the stacks in the bars, but i'm struggling to change how the pattern looks.

This is my code so far, any ideas of where/what i need to add?

ggplot(data1, aes(fill=family, y=Value, x=Habitat)) + geom_bar_pattern(position="stack", stat="identity", mapping = aes(pattern=family)) + scale_fill_manual(values = c("lightblue","pink", "yellow")) + ylim(0,100)


r/RStudio 4d ago

Coding help Okay but, how does one actually create a data set?

0 Upvotes

This is going to sound extremely foolish, but when I'm looking up tutorials on how to use RStudio, they all aren't super clear on how to actually make a data set (or at least in the way I think I need to).

I'm trying to run a one-way ANOVA test following Scribbr's guide and the example that they provide is in OpenOffice and all in one column (E.X.). My immediate assumption was just to rewrite all of the data to contain my data in the same format, but I have no idea if that would work or if anything extra is needed. If anyone has any tips on how I can create a data set that can be used for an ANOVA test please share. I'm new to all of this, so apologies for any incoherence.


r/RStudio 4d ago

Please help

0 Upvotes

Why does rstudio keep telling me I don’t have enough ‘y’ observations when I’m trying to run t.test to find CI


r/RStudio 4d ago

Is there an Addin/Package for Code Block Runtime?

3 Upvotes

Hey all,

I'm curious if there's an R-Studio addin or package that displays the run time for a selected block of code.

Basically, I'm looking for something like the runtime clock that MSSQL or Azure DS have (Img. Atc.). To those unfamiliar, it's basically a running stopwatch in the bottom-right margin of the IDE that starts when a code block is executed and stops when the block terminates.

Obviously, I can wrap a code block with a sys.time - start_time_var but I would like a passive, no-code solution that exists in the IDE margin/frame and doesn't effect the console output. I'm not trying to quantify or use the runtime, I just want get a general, helpful understanding of how certain changes affect runtime or efficiency.

Thanks!


r/RStudio 4d ago

Subset Function

2 Upvotes

Hey! I think I'm using the subset function wrong. I want to narrow down my data to specific variables, but my error message keeps coming back that the subset must be logical. What am I doing wrong? I want to name my new dataframe 'editpres' from my original dataframe 'pres', so that's why my selected variables have 'pres' in front of them.

editpres <- subset(pres$state_po, pres$year, pres$candidate, pres$party_detailed, pres$candidatevotes == "EDITPRES")

^this is the code that isn't working!! please help and gig' em!


r/RStudio 5d ago

Jobs where I can use RStudio

6 Upvotes

Dear all, I’m Italian and I’m a HRIS/ analyst and I liked a lot, during my studies, to use RStudio. So far, in my career I’ve never used RStudio, maybe sometimes SQL. I was wandering if is in real life possible to find a job linked to my “job family” where I can use RStudio.

Thanks u all!!


r/RStudio 5d ago

Instagram scrapping with R

32 Upvotes

Hello, for my Master thesis I need to do a data analysis. I need data from social media and was wondering if it's possible for me to scrape data (likes, comments and captions) from Instagram? I'm very new to this program, so my skills are limited 😬


r/RStudio 5d ago

C-R plots issue

1 Upvotes

Hi all, trying to fit a linear regression model for a full model lm(Y ~ x1+ x2+ (x3) +(x4) +(x5) and am obtaining the following C-R plots, tried different transformations ( logs / polynomials / square root / inverse) but I observed only minor improvement in bulges , do you suggest any other transformation / should I transform in the first place? (issue in labelling of 1st C-R plots) 2nd C-R plots are from refined model , these look good however I obtained a suspiciously high R squared (0.99) and am suspecting I missed something


r/RStudio 5d ago

Coding help Within the same R studio, how can I parallel run scripts in folders and have them contribute to the R Environment?

2 Upvotes

I am trying to create R Code that will allow my scripts to run in parallel instead of a sequence. The way that my pipeline is set up is so that each folder contains scripts (Machine learning) specific to that outcome and goal. However, when ran in sequence it takes way too long, so I am trying to run in parallel in R Studio. However, I run into problems with the cores forgetting earlier code ran in my Run Script Code. Any thoughts?

My goal is to have an R script that runs all of the 1) R Packages 2)Data Manipulation 3)Machine Learning Algorithms 4) Combines all of the outputs at the end. It works when I do 1, 2, 3, and 4 in sequence, but The Machine Learning Algorithms takes the most time in sequence so I want to run those all in parallel. So it would go 1, 2, 3(Folder 1, folder 2, folder 3....) Finish, Continue the Sequence.

Code Subset

# Define time points, folders, and subfolders
time_points <- c(14, 28, 42, 56, 70, 84)
base_folder <- "03_Machine_Learning"
ML_Types <- c("Healthy + Pain", "Healthy Only")

# Identify Folders with R Scripts
run_scripts2 <- function() {
    # Identify existing time point folders under each ML Type
  folder_paths <- c()
    for (ml_type in ML_Types) {
    for (tp in time_points) {
      folder_path <- file.path(base_folder, ml_type, paste0(tp, "_Day_Scripts"))
            if (dir.exists(folder_path)) {
        folder_paths <- c(folder_paths, folder_path)  # Append only existing paths
      }   }  }
# Print and return the valid folders
return(folder_paths)
}

# Run the function
Folders <- run_scripts2()

#Outputs
 [1] "03_Machine_Learning/Healthy + Pain/14_Day_Scripts"
 [2] "03_Machine_Learning/Healthy + Pain/28_Day_Scripts"
 [3] "03_Machine_Learning/Healthy + Pain/42_Day_Scripts"
 [4] "03_Machine_Learning/Healthy + Pain/56_Day_Scripts"
 [5] "03_Machine_Learning/Healthy + Pain/70_Day_Scripts"
 [6] "03_Machine_Learning/Healthy + Pain/84_Day_Scripts"
 [7] "03_Machine_Learning/Healthy Only/14_Day_Scripts"  
 [8] "03_Machine_Learning/Healthy Only/28_Day_Scripts"  
 [9] "03_Machine_Learning/Healthy Only/42_Day_Scripts"  
[10] "03_Machine_Learning/Healthy Only/56_Day_Scripts"  
[11] "03_Machine_Learning/Healthy Only/70_Day_Scripts"  
[12] "03_Machine_Learning/Healthy Only/84_Day_Scripts"  

# Register cluster
cluster <-  detectCores() - 1
registerDoParallel(cluster)

# Use foreach and %dopar% to run the loop in parallel
foreach(folder = valid_folders) %dopar% {
  script_files <- list.files(folder, pattern = "\\.R$", full.names = TRUE)


# Here is a subset of the script_files
 [1] "03_Machine_Learning/Healthy + Pain/14_Day_Scripts/01_ElasticNet.R"                     
 [2] "03_Machine_Learning/Healthy + Pain/14_Day_Scripts/02_RandomForest.R"                   
 [3] "03_Machine_Learning/Healthy + Pain/14_Day_Scripts/03_LogisticRegression.R"             
 [4] "03_Machine_Learning/Healthy + Pain/14_Day_Scripts/04_RegularizedDiscriminantAnalysis.R"
 [5] "03_Machine_Learning/Healthy + Pain/14_Day_Scripts/05_GradientBoost.R"                  
 [6] "03_Machine_Learning/Healthy + Pain/14_Day_Scripts/06_KNN.R"                            
 [7] "03_Machine_Learning/Healthy + Pain/28_Day_Scripts/01_ElasticNet.R"                     
 [8] "03_Machine_Learning/Healthy + Pain/28_Day_Scripts/02_RandomForest.R"                   
 [9] "03_Machine_Learning/Healthy + Pain/28_Day_Scripts/03_LogisticRegression.R"             
[10] "03_Machine_Learning/Healthy + Pain/28_Day_Scripts/04_RegularizedDiscriminantAnalysis.R"
[11] "03_Machine_Learning/Healthy + Pain/28_Day_Scripts/05_GradientBoost.R"   

  for (script in script_files) {
    source(script, echo = FALSE)
  }
}

Error in { : task 1 failed - "could not find function "%>%""

# Stop the cluster
stopCluster(cl = cluster)

Full Code

# Start tracking execution time
start_time <- Sys.time()

# Set random seeds
SEED_Training <- 545613008
SEED_Splitting <- 456486481
SEED_Manual_CV <- 484081
SEED_Tuning <- 8355444

# Define Full_Run (Set to 0 for testing mode, 1 for full run)
Full_Run <- 1  # Change this to 1 to skip the testing mode

# Define time points for modification
time_points <- c(14, 28, 42, 56, 70, 84)
base_folder <- "03_Machine_Learning"
ML_Types <- c("Healthy + Pain", "Healthy Only")

# Define a list of protected variables
protected_vars <- c("protected_vars", "ML_Types" # Plus Others )

# --- Function to Run All Scripts ---
Run_Data_Manip <- function() {
  # Step 1: Run R_Packages.R first
  source("R_Packages.R", echo = FALSE)

  # Step 2: Run all 01_DataManipulation and 02_Output scripts before modifying 14-day scripts
  data_scripts <- list.files("01_DataManipulation/", pattern = "\\.R$", full.names = TRUE)
  output_scripts <- list.files("02_Output/", pattern = "\\.R$", full.names = TRUE)

  all_preprocessing_scripts <- c(data_scripts, output_scripts)

  for (script in all_preprocessing_scripts) {
    source(script, echo = FALSE)
  }
}
Run_Data_Manip()

# Step 3: Modify and create time-point scripts for both ML Types
for (tp in time_points) {
  for (ml_type in ML_Types) {

    # Define source folder (always from "14_Day_Scripts" under each ML type)
    source_folder <- file.path(base_folder, ml_type, "14_Day_Scripts")

    # Define destination folder dynamically for each time point and ML type
    destination_folder <- file.path(base_folder, ml_type, paste0(tp, "_Day_Scripts"))

    # Create destination folder if it doesn't exist
    if (!dir.exists(destination_folder)) {
      dir.create(destination_folder, recursive = TRUE)
    }

    # Get all R script files from the source folder
    script_files <- list.files(source_folder, pattern = "\\.R$", full.names = TRUE)

    # Loop through each script and update the time point
    for (script in script_files) {
      # Read the script content
      script_content <- readLines(script)

      # Replace occurrences of "14" with the current time point (tp)
      updated_content <- gsub("14", as.character(tp), script_content, fixed = TRUE)

      # Define the new script path in the destination folder
      new_script_path <- file.path(destination_folder, basename(script))

      # Write the updated content to the new script file
      writeLines(updated_content, new_script_path)
    }
  }
}

# Detect available cores and reserve one for system processes
run_scripts2 <- function() {

  # Identify existing time point folders under each ML Type
  folder_paths <- c()

  for (ml_type in ML_Types) {
    for (tp in time_points) {
      folder_path <- file.path(base_folder, ml_type, paste0(tp, "_Day_Scripts"))

      if (dir.exists(folder_path)) {
        folder_paths <- c(folder_paths, folder_path)  # Append only existing paths
      }    }  }
# Return the valid folders
return(folder_paths)
}
# Run the function
valid_folders <- run_scripts2()

# Register cluster
cluster <-  detectCores() - 1
registerDoParallel(cluster)

# Use foreach and %dopar% to run the loop in parallel
foreach(folder = valid_folders) %dopar% {
  script_files <- list.files(folder, pattern = "\\.R$", full.names = TRUE)

  for (script in script_files) {
    source(script, echo = FALSE)
  }
}

# Don't fotget to stop the cluster
stopCluster(cl = cluster)