0

I am fairly new to R so excuse my baby steps in plotly. I am trying to compute density curves for total time spent

# Compute density curves
d.Bardoc <- AeDec %>%
  filter(Bardoc == 1) %>%  
  density(AeDec$TotalTimeinAE) ##, na.rm = TRUE) 

But I keep getting the following error:

Error in density.default(., AeDec$TotalTimeinAE) : 
argument 'x' must be numeric  

I've checked that TotalTimeinAE is numeric with str(AeDec):

$ TotalTimeinAE: num  315 94 470 29 17 9 11 101 23 107 ...

What do I need to do to be able to calculate density curve for this variable?

camille
  • 16,432
  • 18
  • 38
  • 60
Visenna
  • 3
  • 1
  • 1
    Hi! And welcome to SO. As it is now, your question is a mess. But I'm sure we'll find good suggestions for you if you find the time to take a look at [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)... – vestland Jan 24 '20 at 22:39
  • ...and learn [how to properly format your question](https://stackoverflow.com/editing-help). Please consider doing that. I can pretty much guarantee you it will be worth your time. – vestland Jan 24 '20 at 22:41
  • 1
    Try to make this [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example); we can't run your code without any of your data, and it's unclear what you're trying to get. I'm not sure what this has to do with plotly at this point, since it looks like you're calling the base `density` function. – camille Jan 24 '20 at 23:25
  • Also note that when you pipe the output of one function as the input to the next, the second function defaults to using the piped input as its first argument. So in this case you've used the filtered data frame as the first argument, whereas the `density` function expects a single numeric vector. And with `dplyr` functions you generally don't want to include `data_frame_name$`, just the column names – camille Jan 24 '20 at 23:26
  • Thank you vestland and camille, too. I will take your comments on board. I am new to stack overflow (previously just used lots of very useful SQL tips and I am miles better at SQL than R! :) – Visenna Jan 25 '20 at 20:14

1 Answers1

0

A couple of options:

1. Try taking density out of pipe:

f.AeDec <- AeDec %>%
  filter(Bardoc == 1) 

d.Bardoc <- density(f.AeDec$TotalTimeinAE)

2. Try selecting your variable and unlist before piping:

d.Bardoc <- AeDec %>%
  filter(Bardoc == 1) %>%
  select(TotalTimeinAE) %>%
  unlist %>%
  density(.)
Ben
  • 28,684
  • 5
  • 23
  • 45