R Q&A 备查(持续更新整理中)

来源:互联网 发布:淘宝店铺违规考试答案 编辑:程序博客网 时间:2024/06/04 18:09


Q:如何生成类似这样的数列Ind_001、Ind_002、...Ind_200

A:  paste("Ind_",sprintf("%03d",c(1:200)),sep="")   核心函数:sprintf()

 

Q:ggplot2的分层语法是什么?

A:图形的分层语法认为每张图都可以包含五个成分:mapping geom stat scale facet
一个带有映射(mapping)的默认数据集;
一个或多个图层,每个包含有几何对象 geom;

统计变换(stat)和和带有映射的数据集;
对每个映射的标度(scale);
坐标系;分面(facet)

Q: table类的处理方式

通过table()或者xtabs()创建列联表,其结果是table类,可以使用ftable()对 table类进行处理。可以使用as.data.frame对table类进行“逆”处理

 

Q:如何做分类描述统计分析

A: aggregate(sat[,2], by=list(sat[,1]), FUN=summary) 注意:by的参数要用list

 

Q:如何计算重复抽样的均值或者方差

A: replicate(100,mean(sample(SATScore, 20, replace=TRUE) ))

 

Q: 合并显示多个字符串。

A:cat("The zero occurs at", 2*pi, "radians.", "\n")

 

Q: Rstudio Representation setting

A:

========================================================
author: Zhichao Luo (Rokia)
date: `r date()`
transition: rotate
width: 1440
height: 900

or autosize: true

 

Q:如何将md文件转换成pdf
A:pandoc -t beamer test.md -o test.pdf

 

 Q; cor(x,y) 结果是NA是怎么回事?

A cor(x, y, method="pearson", use="complete") 要使用use="complete" 将缺失值排除

 

 Q:如何将不同的图表画在一张图里面

A:

 library(grid) a <- ggplot(dsmall, aes(color, price/carat)) + geom_jitter(size=4, alpha = I(1 / 1.5), aes(color=color)) b <- ggplot(dsmall, aes(color, price/carat, color=color)) + geom_boxplot() c <- ggplot(dsmall, aes(color, price/carat, fill=color)) + geom_boxplot() + theme(legend.position = "none") grid.newpage() # Open a new page on grid device pushViewport(viewport(layout = grid.layout(2, 2))) # Assign to device viewport with 2 by 2 grid layout print(a, vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2)) print(b, vp = viewport(layout.pos.row = 2, layout.pos.col = 1)) print(c, vp = viewport(layout.pos.row = 2, layout.pos.col = 2, width=0.3, height=0.3, x=0.8, y=0.8))


 Q:如何在数据集中增加一个新的计算变量

A:

# Three examples for doing the same computations #1mydata$sumx <- mydata$x1 + mydata$x2 mydata$meanx <- (mydata$x1 + mydata$x2)/2  #2attach(mydata)mydata$sumx <- x1 + x2 mydata$meanx <- (x1 + x2)/2 detach(mydata)  #3mydata <- transform(mydata, sumx = x1 + x2, meanx = (x1 + x2)/2  ) 


  

Q:根据某一变量的过滤条件为另外一变量赋值

attach(leadership)leadership$agecat[age > 75] <- "Elder" leadership$agecat[age > 45 & age <= 75] <- "Middle Aged" leadership$agecat[age <= 45] <- "Young" detach(leadership) 


 

 Q:如何画一张很长的时间序列图

 

png("wide.png", width = 1e5, height = 500)plot((sin(1:10000/100)+rnorm(10000)/5),type='l')dev.off()


 

 Q:如何替换缺失数据?

 

library(Hmisc)DF <- data.frame(age = c(10, 20, NA, 40), sex = c('male','female'))# impute with mean valueDF$imputed_age <- with(DF, impute(age, mean))# impute with random valueDF$imputed_age2 <- with(DF, impute(age, 'random'))# impute with the mediawith(DF, impute(age, median))# impute with the minimumwith(DF, impute(age, min))# impute with the maximumwith(DF, impute(age, max))# and if you are sufficiently foolish# impute with number 7 with(DF, impute(age, 7)) # impute with letter 'a'with(DF, impute(age, 'a'))Look at ?impute for details on how the imputation is implemented


 

 Q: 如何连接SQLITE数据文件

library("RSQLite")drv <- dbDriver("SQLite")con <- dbConnect(drv, dbname = "data/test.s3db")dl1<-dbGetQuery(con, "select * from tb1")dl2<-dbGetQuery(con, "select * from tb2")


 Q:The forecast package only works with ts objects?

A: It's a pitty, Yes. Also,hw requires a ts object not a zoo object

 

0 0
原创粉丝点击