已经使用SAS 6年并迁移到R.我曾经使用proc内容来获得对表,特征和数据类型的健康描述.
使用str(tableName)我可以看到数据框中的类型而不是矢量位置.
使用name(tableName)我可以看到矢量的名称和位置,但不能看到类型.
使用summary(tableName)我可以看到分位数/类别,但不能轻易看到类型或向量位置.
有没有办法我可以得到名称vectorPosition类型的列表min max avg med [..]
听起来你可能正在describe()从Hmisc包中寻找类似的东西.我的回忆是,Frank Harrel(该软件包的作者)是一位长期的SAS程序员,很早就来到了R世界.describe()提供的摘要的风格当然似乎反映了计算家谱:
library(Hmisc)
describe(cars) # for example
cars
2 Variables 50 Observations
---------------------------------------------------------------------------------
speed
n missing unique Mean .05 .10 .25 .50 .75 .90
50 0 19 15.4 7.0 8.9 12.0 15.0 19.0 23.1
.95
24.0
4 7 8 9 10 11 12 13 14 15 16 17 18 19 20 22 23 24 25
Frequency 2 2 1 1 3 2 4 4 4 3 2 3 4 3 5 1 1 4 1
% 4 4 2 2 6 4 8 8 8 6 4 6 8 6 10 2 2 8 2
---------------------------------------------------------------------------------
dist
n missing unique Mean .05 .10 .25 .50 .75 .90
50 0 35 42.98 10.00 15.80 26.00 36.00 56.00 80.40
.95
88.85
lowest : 2 4 10 14 16, highest: 84 85 92 93 120
---------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
您可以使用lapply在data.frame的每一列上调用函数,并计算该函数中所需的所有数量.
summary_text <- function(d) {
do.call(rbind, lapply( d, function(u)
data.frame(
Type = class(u)[1],
Min = if(is.numeric(u)) min( u, na.rm=TRUE) else NA,
Mean = if(is.numeric(u)) mean( u, na.rm=TRUE) else NA,
Median = if(is.numeric(u)) median(u, na.rm=TRUE) else NA,
Max = if(is.numeric(u)) max( u, na.rm=TRUE) else NA,
Missing = sum(is.na(u))
)
) )
}
summary_text(iris)
Run Code Online (Sandbox Code Playgroud)
但我个人更喜欢以图形方式查看数据:以下函数将在单个页面上为每个数字变量绘制直方图和分位数 - 分位数图,并为每个因子绘制条形图.如果您有20到30个变量,它应该仍然可用.
summary_plot <- function(d, aspect=1) {
# Split the screen: find the optimal number of columns
# and rows to be as close as possible from the desired aspect ratio.
n <- ncol(d)
dx <- par()$din[1]
dy <- par()$din[2]
f <- function(u,v) {
if( u*v >= n && (u-1)*v < n && u*(v-1) < n ) {
abs(log((dx/u)/(dy/v)) - log(aspect))
} else {
NA
}
}
f <- Vectorize(f)
r <- outer( 1:n, 1:n, f )
r <- which( r == min(r,na.rm=TRUE), arr.ind=TRUE )
r <- r[1,2:1]
op <- par(mfrow=c(1,1),mar=c(2,2,2,2))
plot.new()
if( is.null( names(d) ) ) { names(d) <- 1:ncol(d) }
ij <- matrix(seq_len(prod(r)), nr=r[1], nc=r[2], byrow=TRUE)
for(k in seq_len(ncol(d))) {
i <- which(ij==k, arr.ind=TRUE)[1]
j <- which(ij==k, arr.ind=TRUE)[2]
i <- r[1] - i + 1
f <- c(j-1,j,i-1,i) / c(r[2], r[2], r[1], r[1] )
par(fig=f, new=TRUE)
if(is.numeric(d[,k])) {
hist(d[,k], las=1, col="grey", main=names(d)[k], xlab="", ylab="")
o <- par(fig=c(
f[1]*.4 + f[2]*.6,
f[1]*.15 + f[2]*.85,
f[3]*.4 + f[4]*.6,
f[3]*.15 + f[4]*.85
),
new=TRUE,
mar=c(0,0,0,0)
)
qqnorm(d[,k],axes=FALSE,xlab="",ylab="",main="")
qqline(d[,k])
box()
par(o)
} else {
o <- par(mar=c(2,5,2,2))
barplot(table(d[,k]), horiz=TRUE, las=1, main=names(d)[k])
par(o)
}
}
par(op)
}
summary_plot(iris)
Run Code Online (Sandbox Code Playgroud)