我使用的是带有时间刻度的折线图,最后一点是 12 月 30 日。但是,对于scale_x_date,即使有该月的数据点,一月也会显示在刻度上。
规模确实应该在上个月停止,但我相信这是因为这一天。有没有办法让刻度坚持数据中的实际月份?
我用它来格式化比例:
#with lubridate
data$month <- ymd(data$month)
#in the plot
scale_x_date(date_breaks = "1 month", date_labels ="%b %y")
Run Code Online (Sandbox Code Playgroud)
可重现的例子:
library(lubridate)
library(ggplot2)
cities <- c("city1")
month <- c("2016-08-01", "2016-09-30", "2016-10-30", "2016-11-30", "2016-12-30")
num <- c(23287, 23889, 25026, 26116, 29758)
data <- data.frame(cities, month, num)
data$month <- ymd(data$month)
gg <- ggplot(data, aes(month, num, group = 1)
gg <- gg + geom_line(position = "identity", color="#6baed6")
gg <- gg + scale_y_continuous(expand = (c(0.1,0)))
gg <- gg + scale_x_date(expand = c(0, …Run Code Online (Sandbox Code Playgroud) 我有一个大桌子,我正在尝试使用tidyr和它的长格式重塑,我想改成宽格式.桌子很大,这比我想象的要复杂得多.
该表看起来像这样
Codes areas var1 var2 var3
1111 1010 2 2 34
1112 1010 3 7 18
1113 1010 20 12 11
1114 1010 19 11 22
[...] [...] [...] [...] [...]
1111 1020 14 19 12
1112 1020 10 10 13
Run Code Online (Sandbox Code Playgroud)
目标是使用宽格式的变量获得每个区域一行.
喜欢:
Area 1111Var1 1111Var2 111Var3 1112Var1 1112Var2 1112Var3
1010 2 2 34 3 7 18
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已尝试在tidyr中传播和变异,但没有取得多大成功.
我想扫描 R 中的多个文件中的字符串,并知道哪些文件名具有该字符串。
有没有办法在函数中使用 grep、cat、readLines 之类的东西来做到这一点?
如果我使用以下方式扫描文件:
fileNames <- Sys.glob("*.csv")
Run Code Online (Sandbox Code Playgroud)
那么也许是这样的:
for (f in fileNames) {
stuff <- read.csv(fileName, sep = ",")
grep("string")
}
names(res) <- substr(filenames, 1, 30)
Run Code Online (Sandbox Code Playgroud)
或者更好的是,像这样的循环:
for( f in filenames ){
cat("string", file=f)
}
for( f in filenames) {
cat(readLines(f), sep="\n")
}
Run Code Online (Sandbox Code Playgroud)
这段代码不起作用,我只是想仔细考虑一下。我确信有更好的方法可以做到这一点。这听起来很简单,但我无法做到正确。
我想扫描文件中的字符串,然后输出找到该字符串的文件名。我还没有找到在 R 中执行此操作的示例。
建议?
我想针对给定月份有多个事件的每次出现只过滤时间序列中的最后一个日期条目。
例如,在这样的表中:
obs <- c("A", "B", "A", "B", "A", "B", "A", "B")
date <- c("2017-01-01", "2017-01-01", "2017-02-01", "2017-02-01", "2017-03-01", "2017-03-01", "2017-03-02","2017-03-02")
num <- c(1000, 1800, 2000, 2900, 3000, 3400, 3500, 3400)
dat <- data.frame(obs, date, num)
obs date num
1 A 2017-01-01 1000
2 B 2017-01-01 1800
3 A 2017-02-01 2000
4 B 2017-02-01 2900
5 A 2017-03-01 3000
6 B 2017-03-01 3400
7 A 2017-03-02 3500
8 B 2017-03-02 3400
Run Code Online (Sandbox Code Playgroud)
对“ A”的简单选择是:
x <- dat %>%
filter(obs=="A") %>%
select(obs, …Run Code Online (Sandbox Code Playgroud)