我有以下图表,它基本上是两个分布的直方图并排绘制:
my.barplot <- function( df, title="", ... ) {
df.count <- aggregate( df$outcome, by=list(df$category1,df$outcome), FUN=length )
colnames( df.count ) <- c("category1","outcome","n")
df.total <- aggregate( df.count$n, by=list(df.count$category1), FUN=sum )
colnames( df.total ) <- c("category1","total")
df.dens <- merge(df.count, df.total)
df.dens$dens <- with( df.dens, n/total )
p <- ggplot( df.dens, aes( x=outcome, fill=category1 ), ... )
p <- p + geom_bar( aes( y=dens ), position="dodge" )
p <- p + opts( axis.text.x=theme_text(angle=-90,hjust=0), title=title )
p
}
N <- 50*(2*8*2)
outcome <- sample(ordered(seq(8)),N,replace=TRUE,prob=c(seq(4)/20,rev(seq(4)/20)) ) …
Run Code Online (Sandbox Code Playgroud) 我有一个字符向量,它是一些PDF抓取文件pdftotext
(命令行工具).
一切都(幸福地)很好地排成一列.但是,向量中充斥着一种空格,这些空格使我的正则表达式无效:
> test
[1] "Address:" "Clinic Information:" "Store " "351 South Washburn" "Aurora Quick Care"
[6] "Info" "St. Oshkosh, WI 54904" "Phone: 920?232?0718" "Pewaukee"
> grepl("[0-9]+ [A-Za-z ]+",test)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> dput(test)
c("Address:", "Clinic Information:", "Store ", "351 South Washburn",
"Aurora Quick Care", "Info", "St. Oshkosh, WI 54904", "Phone: 920?232?0718",
"Pewaukee")
> test.pasted <- c("Address:", "Clinic Information:", "Store ", "351 South Washburn",
+ "Aurora Quick Care", "Info", "St. Oshkosh, …
Run Code Online (Sandbox Code Playgroud) 一位电气工程师最近告诫我不要使用GPU进行科学计算(例如,准确性非常重要),因为没有像CPU那样的硬件保护措施.这是真的,如果是这样,典型硬件中的问题有多常见/实质性?
在改进rbind
方法时,我想提取传递给它的对象的名称,以便我可以从中生成唯一的ID.
我试过了,all.names(match.call())
但这只是给了我:
[1] "rbind" "deparse.level" "..1" "..2"
Run Code Online (Sandbox Code Playgroud)
通用示例:
rbind.test <- function(...) {
dots <- list(...)
all.names(match.call())
}
t1 <- t2 <- ""
class(t1) <- class(t2) <- "test"
> rbind(t1,t2)
[1] "rbind" "deparse.level" "..1" "..2"
Run Code Online (Sandbox Code Playgroud)
而我希望能够检索c("t1","t2")
.
我知道一般来说,一个人无法检索传递给函数的对象的名称,但似乎有......可能,如上例中的substitute(...)
返回t1
.
我的数据如下:
完整的数据作为要点.
我试图想象出每个实体的计数比例.为此,我使用了以下代码:
icc <- transform( icc, state=factor(state), entity=factor(entity), type=factor(type) )
p <- ggplot( icc, aes( x=state, y=count, fill=entity ) ) +
geom_bar( stat="identity", position="stack" ) +
facet_grid( type ~ . )
custom_theme <- theme_update(legend.position="none")
p
Run Code Online (Sandbox Code Playgroud)
不幸的是,我丢失了很多信息,因为具有大量实体的状态类型没有显示足够的独特颜色.
如上所述,我有125个实体,但是状态类型中的大多数实体是29.有没有办法强制ggplot2和colorbrewer在每个实体类型中分配一个唯一的(并且希望相当不同)颜色?
到目前为止,我想出的唯一方法是强制entity
转换一个整数,它可以工作但不会在关卡之间提供很多颜色区分.
> within( list(a="a",b="b"), c="c" )
Error in eval(expr, envir, enclos) : argument is missing, with no default
> within( list(a="a",b="b"), c<-"c" )
$a
[1] "a"
$b
[1] "b"
$c
[1] "c"
Run Code Online (Sandbox Code Playgroud)
我不确定为什么这两个不应该是等价的.似乎该=
版本被解释为一个名为c
内部的参数因为...
.有没有办法禁用这种行为?我试过了,
within( list(a="a",b="b"), `c`="c" )
Run Code Online (Sandbox Code Playgroud)
但那也失败了.
我有一个大文本向量我想搜索特定的字符或短语.正则表达式将永远存在.我该如何快速搜索?
样本数据:
R <- 10^7
garbage <- replicate( R, paste0(sample(c(letters[1:5]," "),10,replace=TRUE),collapse="") )
Run Code Online (Sandbox Code Playgroud) 在传统中plyr
,返回的行会自动添加到输出中,即使它们超过了该分组的输入行数:
set.seed(1)
dat <- data.frame(x=runif(10),g=rep(letters[1:5],each=2))
> ddply( dat, .(g), function(df) df[c(1,1,1,2),] )
x g
1 0.26550866 a
2 0.26550866 a
3 0.26550866 a
4 0.37212390 a
5 0.57285336 b
6 0.57285336 b
7 0.57285336 b
8 0.90820779 b
9 0.20168193 c
10 0.20168193 c
11 0.20168193 c
12 0.89838968 c
13 0.94467527 d
14 0.94467527 d
15 0.94467527 d
16 0.66079779 d
17 0.62911404 e
18 0.62911404 e
19 0.62911404 e
20 0.06178627 e
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何做同样的事情dplyr
.一些尝试:
dat …
Run Code Online (Sandbox Code Playgroud) 人口普查局没有提供全国范围的公共使用微观数据区域(美国社区调查中可用的最小地理位置).我尝试将它们全部用几种不同的方法组合在一起,但即使是那些一旦它击中加利福尼亚就会使标识符失效的方法.我是在做傻事还是这需要一个困难的解决方法?这里是重现事情的代码.
library(taRifx.geo)
library(maptools)
td <- tempdir() ; tf <- tempfile()
setInternet2( TRUE )
download.file( "ftp://ftp2.census.gov/geo/tiger/TIGER2014/PUMA/" , tf )
al <- readLines( tf )
tl <- al[ grep( "geo/tiger/TIGER2014/PUMA/tl_2014_" , al ) ]
fp <- gsub( "(.*)geo/tiger/TIGER2014/PUMA/tl_2014_([0-9]*)_puma10\\.zip(.*)" , "\\2" , tl )
# get rid of alaska
fp <- fp[ fp != '02' ]
af <- paste0( "ftp://ftp2.census.gov/geo/tiger/TIGER2014/PUMA/tl_2014_" , fp , "_puma10.zip" )
d <- NULL
for ( i in af ){
try( file.remove( z ) , silent = TRUE ) …
Run Code Online (Sandbox Code Playgroud) 使用dplyr,我想通过一个我可以改变的变量来总结[sic](例如在循环或apply-style命令中).
直接输入名称可以正常工作:
library(dplyr)
ChickWeight %>% group_by( Chick, Diet ) %>% summarise( mw = mean( weight ) )
Run Code Online (Sandbox Code Playgroud)
但是group_by
并没有写一个字符向量,所以传递结果更难.
v <- "Diet"
ChickWeight %>% group_by( c( "Chick", v ) ) %>% summarise( mw = mean( weight ) )
## Error
Run Code Online (Sandbox Code Playgroud)
我会发布一个解决方案,但很想知道其他人如何解决这个问题.