Finn
February 2, 2020, 6:52pm
1
Hi! I'm very new to R, so sorry if this is an obvious question. I'm trying to visualise a relationship between weight (numeric) and sex (factor), however I only want to look at males and not all entries within the sex factor.
How do I have R only consider one value within a column? Should I use the drop function? If anyone could point me in the right direction that would be awesome.
Thank you, sorry if anything isn't clear.
rama27
February 2, 2020, 8:01pm
2
One solution might be to subset males into new dataframe. Another solution is to create a column with dummy variables that equals one for males.
For example:
df$Male <- ifelse(df$Sex == "male", 1, 0)
And now try your ggplot.
Here is an example with made-up sample data (BTW you should make your questions providing a REPR oducible EX ample (reprex) as the one bellow).
library(tidyverse)
set.seed(123) # For reproducibility
sample_data <- data.frame(
weight = runif(10, 50, 80),
sex = sample(c("Male", "Female"), size = 20, replace = TRUE)
)
sample_data %>%
filter(sex == "Male") %>%
ggplot(aes(y = weight, x = sex)) +
geom_boxplot()
Created on 2020-02-02 by the reprex package (v0.3.0)
Finn
February 3, 2020, 11:56pm
4
I completely forgot about subsets, shows what a novice I am!
NoFemale <- FishData %>% filter(Sex == "male")
seemed to work for my purposes but I'm unsure if this is the best way. I'll try your other suggestions method as well.
system
Closed
February 24, 2020, 11:56pm
5
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.