r

How to calculate cumulative mean in R? [duplicate]

不问归期 提交于 2021-02-16 15:19:06
问题 This question already has answers here : Calculate cumulative average (mean) (7 answers) Closed 3 years ago . (I am sorry if the term is not correct). In R, I have a numeric vector x . I want to create new vector y where: y[i] = mean (x[1:i) It is easy to write a function to calculate y , but is there a built-in function in R which do the task? Thank you very much 回答1: Try this y <- cumsum(x) / seq_along(x) Reference https://stat.ethz.ch/pipermail/r-help/2008-May/162729.html 来源: https:/

How can I make an R plot use the Latin Modern font family when saved as a PDF?

做~自己de王妃 提交于 2021-02-16 15:14:51
问题 If I type par(family='CM Roman') then I can get a plot to display with the Computer Modern font. However, when I try to save it as a PDF, it comes up as a blank document, unlike if I take out that line. How can I get it to save correctly as a PDF? Also, how can I use Latin Modern instead of Computer Modern? 回答1: This is how I did it in Windows: Install the extrafont package. Install Latin Modern fonts, e.g. from http://www.fontsquirrel.com/fonts/latin-modern-roman. Watch out, you need to

How to reorder a data frame based on mean of column groups

末鹿安然 提交于 2021-02-16 15:08:42
问题 I am trying to reorder a data frame based on the median value associated with a column ID. I have a dataframe with a column of IDs and 2 columns of values. ID <- c("a","a","a","b","b","b","c","c","c","c") alpha <- c(3,4,5,9,11,13,1,1,1,0) beta <- c(2,3,4,3,4,5,4,5,6,7) df <- data.frame(ID,alpha,beta) ID alpha beta 1 a 3 2 2 a 4 3 3 a 5 4 4 b 9 3 5 b 11 4 6 b 13 5 7 c 1 4 8 c 1 5 9 c 1 6 10 c 0 7 I want to reorder this dataframe so that the column ID is in an order based on the decending means

How to reorder a data frame based on mean of column groups

大城市里の小女人 提交于 2021-02-16 15:07:31
问题 I am trying to reorder a data frame based on the median value associated with a column ID. I have a dataframe with a column of IDs and 2 columns of values. ID <- c("a","a","a","b","b","b","c","c","c","c") alpha <- c(3,4,5,9,11,13,1,1,1,0) beta <- c(2,3,4,3,4,5,4,5,6,7) df <- data.frame(ID,alpha,beta) ID alpha beta 1 a 3 2 2 a 4 3 3 a 5 4 4 b 9 3 5 b 11 4 6 b 13 5 7 c 1 4 8 c 1 5 9 c 1 6 10 c 0 7 I want to reorder this dataframe so that the column ID is in an order based on the decending means

Create ggplot2 legend for multiple datasets

寵の児 提交于 2021-02-16 14:55:09
问题 I am trying to display background data in grey in a ggplot with legend automatically. My aim is to either include the grey datapoints in the legend, or to make a second legend with a manual title. However I fail at doing any of the two. My data is in long format. require(ggplot2) xx<-data.frame(observation="all cats",x=1:2,y=1:2) yy<-data.frame(observation=c("red cats","blue cats"),x=3:4,y=3:4) g<-ggplot() + geom_point(aes(x,y, colour=factor(observation)), colour="grey60", size=5, data=xx) +

add column to my data frame listing columns with the highest row value

前提是你 提交于 2021-02-16 14:54:12
问题 trying tell r to read through the rows of my dataframe and add the column with the highest value in the row to a new column in the dataframe called "MOST_COMMON_CANCER" I tried the following code but got an error. BASE_DF2 <- BASE_DF2%>%mutate(MOST_COMMON_CANCER=colnames(BASE_DF2[8:26])[max.col(BASE_DF2[8:26],ties.method="first")],.keep="all",.after=c_INCS_RATE) Error: Problem with `mutate()` input `MOST_COMMON_CANCER`. x Input `MOST_COMMON_CANCER` can't be recycled to size 1. i Input `MOST

R Kruskal-Wallis with multiple factors

。_饼干妹妹 提交于 2021-02-16 14:52:38
问题 I'm looking for help on performing the Kruskal-Wallis test on my set of data for a large number of factors. I can perform the test for a single factor, like AD_1yr: kruskal.test(Shannon ~ AD_1y, data=comm) But I have over 50 factors I want to test, and was hoping there is a code I can enter that will perform the test for all the factors without having to manually perform the test 50 different times. 回答1: We can use lapply to loop over the factor columns, create a data.frame with the 'shannon'

R - convert POSIXct to fraction of julian day

假装没事ソ 提交于 2021-02-16 14:46:41
问题 How can a date/time object in R be transformed on the fraction of a julian day? For example, how can I turn this date: date <- as.POSIXct('2006-12-12 12:00:00',tz='GMT') into a number like this > fjday [1] 365.5 where julian day is elapsed day counted from the january 1st. The fraction 0.5 means that it's 12pm, and therefore half of the day. This is just an example, but my real data covers all the 365 days of year 2006. 回答1: Since all your dates are from the same year (2006) this should be

Warning in gam with release of R 3.6.1

。_饼干妹妹 提交于 2021-02-16 14:38:25
问题 I now see a warning when running gam() . MWE below: library(gam) data(Boston, package = 'MASS') model.gam <- gam(formula = medv ~ crim + zn, data = Boston) The warning I receive is: Warning message: In model.matrix.default(mt, mf, contrasts) : non-list contrasts argument ignored I think this may relate to a change in the model.matrix() function in the latest R release: https://cran.r-project.org/doc/manuals/r-release/NEWS.html. I did not receive a warning earlier today when running R version

how to place legend horizontaly above barplot in r

霸气de小男生 提交于 2021-02-16 14:37:31
问题 I have to place my legend horizontally above the barplot. I have search a lot but could not found a statisfactory answer. I have found an answer here but it does not solve my problem. 回答1: I don't know what your data looks like and what you want your legend to be but horizontal legends are produced by setting horiz = TRUE . You can place a legend on the top of a plot using "top" as the legend position. If you want it outside your plot region you can move it upward using inset (you'll have to