Create a plot from boxplot.stats [closed] - r

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Someone sent me a file containing the list of boxplot.stats.
I now want to reproduce and plot this boxplot from the list. (I have stats, n , conf and out).
How should I proceed? Can I use plotly for this purpose?
So I have the following list
stats
[1] -0.30518460 0.08578944 0.28487839 0.34645644 0.73711925
n
[1] 3472096
conf
[1] 0.2846574 0.2850994
out
[1] -2.5168701 -0.3115725 0.7683801 1.9771345 -0.5612497 -1.0996948
And my output should be boxplot with the values above.

If by boxplot.stats you mean the list of values boxplot normally produces, you can use bxp to plot.
x <- boxplot(rnorm(100))
bxp(x)
The boxplot.stats function does the computation for boxplot, but doesn't work with bxp directly.
bxp(boxplot.stats(rnorm(100)))
Error in z$stats[, i] : incorrect number of dimensions
If you do just have the output from boxplot.stats, you can hack out a solution by converting each of the list elements to a 1 column matrix:
bxp(lapply(boxplot.stats(rnorm(100)),as.matrix,1))

Related

Correlation coefficient returns N/A. Why? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have a question regarding the correlation coefficient.
Why, if both variables are numeric, does the coefficient give me N/A? Thanks
When I test different variables in a dependent, on several occasions I get N/A as a result. This happens when I do it between a numeric dependent and independent variable.
There is likely two possible reasons
One of the variables is constant
There are NA in your data, if so:
In R there are two functions that compute the pearson correlation, let's see an example.
Data
x <- rnorm(10)
y <- x;y[1] <- NA
There is the cor function
cor(x,y)
that will result in a NA by default. But if you change the argument use
cor(x,y,use = "na.or.complete")
It will result in 1. Another way is to use the function cor.test, that by default ignores missing values.
cor.test(x,y)
But since is a test function, the output is a list object. If you only want the coefficient. you can get the value, by:
cor.test(x,y)$estimate

Plotting measurements over time in R [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am trying to create a plot in R that shows post-surgical outcomes over time. I want to plot a certain data point at pre-op, 1 month post-op, 6 months post-op, etc. Here is an example dataframe:
dat <- data.frame(Preop=c(-2,0.5,-0.25,1.5), PO_1M=c(-1.5,0.2,-0.1,1.0), PO_6M=c(-1.2,0.1,-0.05,0.5), PO_1Y=c(-1.0,0.05,0,0.25))
dat
Ideally, the x axis will have markings for the time (preop, 1 month post-op, etc.), and the y axis will have the value at that time. The data should converge around y=0 coming from either the positive or negative direction, and I imagine a plot looking something like this:
My actual dataframe also has many missing values, so this would need to be accounted for somehow. I would appreciate if anyone could help approach this problem using either ggplot or base R plotting functions. Thanks so much!
Your data should be restructured. Use tidyr package to help make your columns into rows. Then use ifelse logic to convert your column names into the number of months. I assigned pre-op to zero months.
library(tidyverse)
dat2<-dat %>% tidyr::pivot_longer(cols=Preop:PO_1Y)
dat2$nummonths<-ifelse(dat2$name=='Preop',0,
ifelse(dat2$name=='PO_1M',1,
ifelse(dat2$name=='PO_6M',6,
ifelse(dat2$name=='PO_1Y',12,NA))))
ggplot(dat2, aes(nummonths,value))+geom_point()+theme_dark()

How can I compare and represent the categorical data of my two columns with 0 if the rows are the same and 1 if not, using R [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
How can I compare the categorical data of my two columns and visualize it with a 0 if the row is the same for the two columns and if different 1.
If the categorical columns of your dataframe are named x and y and have the same levels you can assign a new column to your dataframe with the result of comparing the two columns using the ifelse function.
df$match <- ifelse(df$x == df$y, 0, 1)

Using Apply() Function to a matrix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Given a table I need to use apply() to find t the correlation between each one of the 8 variables in the state.x77 matrix and the Population variable. state.x77 is a built in matrix with 8 columns.
I had to first create a function called cor_var due to the instructions and then have to use apply(). So here is my input:
cor_var=function(v1,v2=state.x77[,"Income"]){cor(v1,v2)}
apply(mat,2,cor_var,v2=state.x77[,"Population"])
the v2 is the extra optional argument for apply() ... argument, so this should work but it is returning Error in cor(v1, v2) : incompatible dimensions. Any help on where I am wrong would be appreciated. I have to use cor_var and apply two functions btw, can't use lappy or mapply.
You can use :
apply(state.x77,2,function(x) cor(x, state.x77[,"Income"]))
#Population Income Illiteracy Life Exp Murder HS Grad Frost Area
# 0.2082276 1.0000000 -0.4370752 0.3402553 -0.2300776 0.6199323 0.2262822 0.3633154
We can use
apply(mat,2,cor_var,v2=state.x77[,"Population"])

histogram of letter grades [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to make a histogram of grades. Here are my variables.
> grade <- factor(c("A","A","A","B","A","A","A","A","B","A","C","B","B","B"))
> numberBook <- c(53,42,40,40,39,34,34,30,28,24,22,21,20,16)
But when I plot it, I get an error message.
> hist(numberBook~grade)
Error in hist.default(numberBook ~ grade) : 'x' must be numeric
What can I do?
I'm not sure why you've got multiple letters so I've guessed that you want a total of all the A, B and Cs. This may not be quite right. I've recreated your data like this using rep and summing the counts of grades (could be wrong)
data <-c(rep("A",(53+42+40+34+34+30+28+22)), rep("B",(39+24+20+16+22)),rep("C",22))
Then I can plot the data using barplot:
barplot(prop.table(table(data)))
Barplot is probably what you want here.

Resources