有没有办法geom_bar()在下面的时间序列示例中丢失数据的情况下设置恒定宽度?我试过设置width在aes()没有运气.在代码示例下方的图中比较5月'11至6月'11的条形宽度.
colours <- c("#FF0000", "#33CC33", "#CCCCCC", "#FFA500", "#000000" )
iris$Month <- rep(seq(from=as.Date("2011-01-01"), to=as.Date("2011-10-01"), by="month"), 15)
colours <- c("#FF0000", "#33CC33", "#CCCCCC", "#FFA500", "#000000" )
iris$Month <- rep(seq(from=as.Date("2011-01-01"), to=as.Date("2011-10-01"), by="month"), 15)
d<-aggregate(iris$Sepal.Length, by=list(iris$Month, iris$Species), sum)
d$quota<-seq(from=2000, to=60000, by=2000)
colnames(d) <- c("Month", "Species", "Sepal.Width", "Quota")
d$Sepal.Width<-d$Sepal.Width * 1000
g1 <- ggplot(data=d, aes(x=Month, y=Quota, color="Quota")) + geom_line(size=1)
g1 + geom_bar(data=d[c(-1:-5),], aes(x=Month, y=Sepal.Width, width=10, group=Species, fill=Species), stat="identity", position="dodge") + scale_fill_manual(values=colours)
Run Code Online (Sandbox Code Playgroud)

这是我作为data.frame的一部分的布尔样本:
atest <- c(FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
FALSE)
我想从每个FALSE返回一个从1开始的数字序列,然后增加1直到下一个FALSE.
得到的所需矢量是:
[1] 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1
Run Code Online (Sandbox Code Playgroud)
下面是实现此代码,但我敢肯定有一个更简单或更优雅的方式在R.做到这一点,我一直在努力学习如何R中更高效地编写代码的东西,而不是简单地完成工作.
result <- c()
x <- 1
for(i in 1:length(atest)){
if(atest[i] == FALSE){
result[i] <- 1
x <- 1
}
if(atest[i] != FALSE){
x <- x+1
result[i] <- x
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用地图库和ggplot的geom_polygon创建地图.我只想将默认的蓝色,红色,紫色配色方案更改为其他方式.我对ggplot非常新,所以如果我没有使用正确的数据类型,请原谅.以下是我使用的数据:
> head(m)
region long lat group order subregion Group.1 debt.to.income.ratio.mean ratio total
17 alabama -87.46201 30.38968 1 1 <NA> alabama 12.4059 20.51282 39
18 alabama -87.48493 30.37249 1 2 <NA> alabama 12.4059 20.51282 39
19 alabama -87.52503 30.37249 1 3 <NA> alabama 12.4059 20.51282 39
20 alabama -87.53076 30.33239 1 4 <NA> alabama 12.4059 20.51282 39
21 alabama -87.57087 30.32665 1 5 <NA> alabama 12.4059 20.51282 39
22 alabama -87.58806 30.32665 1 6 <NA> alabama 12.4059 20.51282 39
> …Run Code Online (Sandbox Code Playgroud) 我正在尝试解析特定元标记中的内容.这是元标记的结构.前两个用反斜杠关闭,但其余的没有任何结束标记.只要我获得第3个元标记,就会<head>返回标记之间的全部内容.我也尝试了,soup.findAll(text=re.compile('keyword'))但由于关键字是元标记的属性,因此不会返回任何内容.
<meta name="csrf-param" content="authenticity_token"/>
<meta name="csrf-token" content="OrpXIt/y9zdAFHWzJXY2EccDi1zNSucxcCOu8+6Mc9c="/>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<meta content='en_US' http-equiv='Content-Language'>
<meta content='c2y_K2CiLmGeet7GUQc9e3RVGp_gCOxUC4IdJg_RBVo' name='google-site- verification'>
<meta content='initial-scale=1.0,maximum-scale=1.0,width=device-width' name='viewport'>
<meta content='notranslate' name='google'>
<meta content="Learn about Uber's product, founders, investors and team. Everyone's Private Driver - Request a car from any mobile phone—text message, iPhone and Android apps. Within minutes, a professional driver in a sleek black car will arrive curbside. Automatically charged to your credit card on file, tip included." name='description'>
Run Code Online (Sandbox Code Playgroud)
这是代码:
import csv …Run Code Online (Sandbox Code Playgroud) 我最初尝试使用geom_raster和geom_tile创建此背景,而是使用图像,因为我认为它会更容易.
但是,我无法让图像占据整个背景,所以点正确地叠加在它上面.背景图片如下:http://imgur.com/a/eZGAP
这是一个dput数据:
structure(list(Industry.Group = c("Defense / Military", "Energy",
"Financial Services", "Healthcare", "Manufacturing", "Public Sector (non-defense) / Not For Profit",
"Retail", "Services", "Technology", "Telecommunications", "Transportation"
), ciq_tech = c(55.9, 53.7111111111111, 60.743661971831, 45.0620689655172,
42.1529411764706, 52.2444444444444, 40.8, 52.3263157894737, 50.9222222222222,
59.5111111111111, 39.6666666666667), Industry.Group = c("Defense / Military",
"Energy", "Financial Services", "Healthcare", "Manufacturing",
"Public Sector (non-defense) / Not For Profit", "Retail", "Services",
"Technology", "Telecommunications", "Transportation"), ciq_org = c(39.6666666666667,
48.7413636363636, 48.0868442622951, 42.2482222222222, 39.0128260869565,
38.482, …
在使用ggplot2进行绘图并尝试根据data.frame中的数字变量重新排序因子时,我似乎总是遇到这个问题.
structure(list(Team = structure(1:32, .Label = c("ARI", "ATL",
"BAL", "BUF", "CAR", "CHI", "CIN", "CLE", "DAL", "DEN", "DET",
"GB", "HOU", "IND", "JAC", "KC", "MIA", "MIN", "NE", "NO", "NYG",
"NYJ", "OAK", "PHI", "PIT", "SD", "SEA", "SF", "STL", "TB", "TEN",
"WAS"), class = "factor"), Fans = c(49L, 145L, 175L, 75L, 104L,
167L, 101L, 147L, 157L, 304L, 112L, 338L, 200L, 118L, 37L, 60L,
65L, 225L, 371L, 97L, 163L, 87L, 84L, 102L, 111L, 85L, 422L,
311L, 63L, 56L, 49L, 271L)), .Names = c("Team", "Fans"), row.names …Run Code Online (Sandbox Code Playgroud) 我正在测试PhantomJS并试图返回angel.co上列出的所有初创公司.我决定使用PhantomJS,因为我需要通过单击底部的"Next"来分页.现在这段代码不会返回任何结果.我是PhantomJS的新手,并已阅读所有代码示例,因此我们非常感谢任何指导.
var page = require('webpage').create();
page.open('https://angel.co/startups', function(status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
page.evaluate(function() {
var list = document.querySelectorAll('div.resume');
for (var i = 0; i < list.length; i++){
console.log((i + 1) + ":" + list[i].innerText);
}
});
}
phantom.exit();
});
Run Code Online (Sandbox Code Playgroud) 这似乎应该更容易,我相信有人可以帮助我.我正在尝试使用lubridate包中的floor_date()将日期的data.frame更改为每个日期到其各自月份的第一个日期,但是其中一些日期是NA.我宁愿不用NA代替虚拟日期.
我试过以下:
library(lubridate)
a<-c(as.Date("2011-05-04"), as.Date("2011-06-12"))
b<-c(as.Date("2012-03-01"), NA)
test <- data.frame(a,b)
apply(test, 1, function(y) sapply(y, function(x) if(!is.na(x)) floor_date(x, "month") else na.pass(x)))
apply(test, 1, function(y) ifelse(!is.na(y)), floor_date(y, "month"), na.pass(y))
Run Code Online (Sandbox Code Playgroud)
第一个调用返回:
Error in object[[name, exact = TRUE]] : subscript out of bounds
Run Code Online (Sandbox Code Playgroud)
第二个调用返回:
Error in update.default(x, mdays = 1, hours = 0, minutes = 0, seconds = 0) :
need an object with call component
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助!
我在这里遵循这个例子,希望使用EC2/S3/EMR/R成功运行. https://gist.github.com/406824
流程步骤中的作业失败.以下是错误日志:
控制器:
2011-07-21T19:14:27.711Z INFO Fetching jar file.
2011-07-21T19:14:30.380Z INFO Working dir /mnt/var/lib/hadoop/steps/1
2011-07-21T19:14:30.380Z INFO Executing /usr/lib/jvm/java-6-sun/bin/java -cp /home/hadoop/conf: /usr/lib/jvm/java-6-sun/lib/tools.jar:/home/hadoop:/home/hadoop/hadoop-0.20-core.jar:/home/hadoop/hadoop-0.20-tools.jar:/home/hadoop/lib/*:/home/hadoop/lib/jetty-ext/* -Xmx1000m -Dhadoop.log.dir=/mnt/var/log/hadoop/steps/1 -Dhadoop.log.file=syslog -Dhadoop.home.dir=/home/hadoop -Dhadoop.id.str=hadoop -Dhadoop.root.logger=INFO,DRFA -Djava.io.tmpdir=/mnt/var/lib/hadoop/steps/1/tmp -Djava.library.path=/home/hadoop/lib/native/Linux-i386-32 org.apache.hadoop.util.RunJar /home/hadoop/contrib/streaming/hadoop-streaming.jar -cacheFile s3n://emrexample21/calculatePiFunction.R#calculatePiFunction.R -input s3n://emrexample21/numberList.txt -output s3n://emrout/ -mapper s3n://emrexample21/mapper.R -reducer s3n://emrexample21/reducer.R
2011-07-21T19:16:12.057Z INFO Execution ended with ret val 1
2011-07-21T19:16:12.057Z WARN Step failed with bad retval
2011-07-21T19:16:14.185Z INFO Step created jobs: job_201107211913_0001
Run Code Online (Sandbox Code Playgroud)
标准错误:
Streaming Command Failed!
Run Code Online (Sandbox Code Playgroud)
标准输出:
packageJobJar: [/mnt/var/lib/hadoop/tmp/hadoop-unjar2368654264051498521/] [] /mnt/var/lib/hadoop/steps/2/tmp/streamjob1658200878131882888.jar tmpDir=null
Run Code Online (Sandbox Code Playgroud)
系统日志:
2011-07-21 19:50:29,539 INFO org.apache.hadoop.mapred.JobClient (main): Default number …Run Code Online (Sandbox Code Playgroud) 我正在尝试计算时间序列中值的标准差,但我希望每次从初始日期值提前一天逐步增加.我知道有一种方法可以在R中执行此操作(可能使用ddply?),这不涉及令人讨厌的for循环.谢谢你的帮助!
d<-seq(from=as.Date("2013-01-01"), to=as.Date("2013-02-01"), by="day")
v <-rnorm(32, 10, 5)
test.df<-data.frame(the_date=d, value=v)
Run Code Online (Sandbox Code Playgroud)
这就是我现在这样做的方式.
result <- c()
for(i in 2:nrow(test.df)){ result[i-1] <- sd(test.df[1:i,]$value)}
Run Code Online (Sandbox Code Playgroud) 我只想尝试使用rJava从R中访问自定义java类的简单示例.
HelloWorld.java
class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
Run Code Online (Sandbox Code Playgroud)
将.java编译为.class如下:
javac HelloWorld.java
Run Code Online (Sandbox Code Playgroud)
R代码(从HelloWorld.java和HelloWorld.class的相同目录运行.
library(rJava)
> .jinit()
[1] 0
> .jnew("HelloWorld")
Error in .jnew("HelloWorld") : java.lang.ClassNotFoundException
Run Code Online (Sandbox Code Playgroud)
谢谢你的任何指示.
我有一个如下所示的数据结构:
groupA1 groupA2 groupB1 groupB2 date text
0 1 1 1 2013-01-01 the dog
Run Code Online (Sandbox Code Playgroud)
对于每一个groupB变量,我想列出一排为具有值1.我需要列出的所有组合每列groupA和groupB其中1秒都存在成一排,但随后也加入日期和文字到每台组合作为该行中的列.
转换后的数据显示为:
var_groupB var_groupA date text
groupB1 groupA2 2013-01-01 the dog
groupB2 groupA2 2013-01-01 the dog
Run Code Online (Sandbox Code Playgroud)
我试过的组合melt和ddply,但我始终没有我需要的变量之一离开了.
有一件事我试过了melt(x, id.vars=c("text", "date")),但后来我失去了约之间的关系的所有信息groupA和groupB.
我可以使用凌乱的循环完成此操作,但不确定是否reshape存在我不知道并且可以完成工作的实用程序.