我有一个数据帧:
dat<- data.frame(date = c("2015-01-01","2015-01-01","2015-01-01", "2015-01-01","2015-02-02","2015-02-02","2015-02-02","2015-02-02","2015-02-02"), val= c(10,20,30,50,300,100,200,200,400), type= c("A","A","B","C","A","A","B","C","C") )
dat
date val type
1 2015-01-01 10 A
2 2015-01-01 20 A
3 2015-01-01 30 B
4 2015-01-01 50 C
5 2015-02-02 300 A
6 2015-02-02 100 A
7 2015-02-02 200 B
8 2015-02-02 200 C
9 2015-02-02 400 C
Run Code Online (Sandbox Code Playgroud)
我希望每天有一行按类型平均,所以输出将是:
Date A B C
2015-01-01 15 30 50
2015-02-02 200 200 300
Run Code Online (Sandbox Code Playgroud)
另外我如何获得计数,结果如下:
Date A B C
2015-01-01 2 1 1
2015-02-02 2 1 2
Run Code Online (Sandbox Code Playgroud) 我有一个数据框:
d<-data.frame(time = factor(c("00:00","00:15","00:30","00:45", "01:00","01:15","01:30","01:45","02:00","02:40" )), q=c(0,0,100,0,0,100,0,0,0,0),p=c(.25,.25,.25,.25,.25,.25,.25,.25,.25,.25))
d
time q p
1 00:00 0 0.25
2 00:15 0 0.25
3 00:30 100 0.25
4 00:45 0 0.25
5 01:00 0 0.25
6 01:15 100 0.25
7 01:30 0 0.25
8 01:45 0 0.25
9 02:00 0 0.25
10 02:40 0 0.25
Run Code Online (Sandbox Code Playgroud)
我想消除列“q”的第一个非零索引之前和列“q”的最后一个非零索引之后的数据框行。在上述情况下,结果应如下所示:
00:30 100 0.25
00:45 0 0.25
01:00 0 0.25
01:15 100 0.25
Run Code Online (Sandbox Code Playgroud)
做到这一点的最佳方法是什么?
我有一个“mydata”数据框,它有 2 列:一个日期列和一个值列。我想将列附加到数据框中,这些列是各个窗口中“值”列的移动平均值。
目前 mydata 有 2 列
Column 1 - date
Column 2 - value
Run Code Online (Sandbox Code Playgroud)
我希望第 3 列是值列的 2 周期移动平均值,第 4 列是值列的 3 周期移动平均值,等等。直到最后一列是 9 个周期的移动平均线
所以我想添加这些列:
Column 3 - SMA(value, 2)
Column 4 - SMA(value, 3)
Column 5 - SMA(value, 4)
Column 6 - SMA(value, 5)
Column 7 - SMA(value, 6)
Column 8 - SMA(value, 7)
Column 9 - SMA(value, 8)
Column 10 - SMA(value, 9)
Run Code Online (Sandbox Code Playgroud)
这是代码
library(TTR)
date = seq(as.Date("2016-01-01"),as.Date("2016-01-10"),"day")
value =c(1,2,3,4,5,6,7,8,9,10)
mydata = data.frame (date, value) …Run Code Online (Sandbox Code Playgroud) 我有一个来自 sql 查询的日期作为一个因素,我想将其设为 POSIXct
f<- as.factor("01/03/2014 20:59:30")
f
class(f)
po<-as.POSIXct(f)
po
Run Code Online (Sandbox Code Playgroud)
结果
> f<- as.factor("01/03/2014 20:59:30")
> f
[1] 01/03/2014 20:59:30
Levels: 01/03/2014 20:59:30
> class(f)
[1] "factor"
> po<-as.POSIXct(f)
> po
[1] "0001-03-20 LMT"
Run Code Online (Sandbox Code Playgroud)
您可以看到“0001-03-20 LMT”不正确。你知道如何将此因子转换为 POSIXct 吗?
谢谢
我想从xlsx工作簿导入工作表,所以我尝试:
library(xlsx)
data<-read.xlsx("m.xlsx", sheetName ="me", stringsAsFactors = FALSE)
Run Code Online (Sandbox Code Playgroud)
并得到错误
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
java.lang.OutOfMemoryError: Java heap space
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能导入这个文件?
此xlsx工作簿包含500行和40列
我在下面有一个数据向量,我想计算一个2周期移动平均值,因为数据中有NA.所以我要找的结果是:
Data 2 period moving average
NA
1 1
2 1.5
3 2.5
4 3.5
5 4.5
6 5.5
7 6.5
8 7.5
9 8.5
10 9.5
NA 10
Run Code Online (Sandbox Code Playgroud)
在r我想要使用roll apply但我没有得到正确的结果:
z <- c(NA,seq(1,10,1),NA)
z
rollapply(z, 2, mean, na.rm = TRUE, fill = list(NA, NULL, NA))
[1] 1.0 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.0 NA
Run Code Online (Sandbox Code Playgroud)
结果中应该只有1个NA,它应该是第一个元素,因为你不能用1个元素做两个周期平均值,但上面的结果显示NA是最后一个元素
我的结果应该是:
NA
1
1.5
2.5
3.5
4.5
5.5
6.5
7.5
8.5
9.5
10
Run Code Online (Sandbox Code Playgroud) 我有这个条形图
group = c("A","A","B","B")
value = c(25,-75,-40,-76)
day = c(1,2,1,2)
dat = data.frame(group = group , value = value, day = day)
dat
ggplot(data=dat, aes(x=factor(group), y=value, fill=factor(day))) +
geom_bar( stat="identity", position="dodge")+
geom_label(aes(label =round(value,0),fill="white"),
colour = "black", position= position_dodge(width=1))
Run Code Online (Sandbox Code Playgroud)
我希望标签为带有黑色字体的白色背景,但是当我添加fill="white"绘图时是不正确的。标签没有白色背景和黑色字体。
注意这里没有fill="white"情节看起来不错。我只想更改标签背景和字体
group = c("A","A","B","B")
value = c(25,-75,-40,-76)
day = c(1,2,1,2)
dat = data.frame(group = group , value = value, day = day)
ggplot(data=dat, aes(x=factor(group), y=value, fill=factor(day))) +
geom_bar( stat="identity", position="dodge")+
geom_label(aes(label =round(value,0)),colour = "black",
position= position_dodge(width=1))
Run Code Online (Sandbox Code Playgroud)
还请注意 …
在我的 R 文件中,我有:
library(rmarkdown)
rmarkdown::render("C://Users//me//Desktop//test_param.Rmd",
params = list(region = "west"))
Run Code Online (Sandbox Code Playgroud)
并在 rmd 文件中:
---
title: "test"
output: pdf_document
params:
name: "test"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
`r params$name`
Run Code Online (Sandbox Code Playgroud)
当我单独运行时,该 rmd 有效。
但是当我运行 .r 文件时,我收到此错误
Error in knit_params_get(input_lines, params) :
render params not declared in YAML: region
Run Code Online (Sandbox Code Playgroud)
这个错误的原因是什么?
我正在模拟按钮点击和启动画面.您可以运行此代码并看到它几乎可以工作.
我的视图看起来像:
@model MvcApplication1.Models.ReportModel
@using Newtonsoft.Json;
@{
ViewBag.Title = "Home Page";
}
@section featured {
<div id="divLoading" style="margin: 0px; padding: 0px; position: fixed; right: 0px;
top: 0px; width: 100%; height: 100%; background-color: #666666; z-index: 30001;
opacity: .8; filter: alpha(opacity=70);display:none">
<p style="position: absolute; top: 30%; left: 45%; color: White;">
Running reports, please wait...<img src="../../Images/ajax-loading.gif">
</p>
Run Code Online (Sandbox Code Playgroud)
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
<div id="inputContainer">
</div>
<div id="reportContainer">
Run report
<span data-bind="text: Date"></span>
<table>
<tr style="font-weight:bold">
<td>
Report Name
</td> …Run Code Online (Sandbox Code Playgroud) 好的,我有你可以在下面运行的代码.我想修改这段代码:
https://gist.github.com/wch/5436415/
这样就可以使用UI的输入动态设置max_plots.UI的输入也是动态的.
这是我的UI.R:
shinyUI(pageWithSidebar(
headerPanel("Dynamic number of plots"),
sidebarPanel(
selectInput("Desk", "Desk:" , c("A","B")),
uiOutput("choose_Product"),
uiOutput("choose_File1"),
uiOutput("choose_Term1"),
sliderInput("n", "Number of plots", value=1, min=1, max=5)
),
mainPanel(
# This is the dynamic UI for the plots
h3("Heading 1"),
uiOutput("plots"),
h3("Heading 2")
)
))
Run Code Online (Sandbox Code Playgroud)
这是我的Server.R:
shinyServer(function(input, output) {
output$choose_Product <- renderUI({
selectInput("Product", "Product:", as.list( c("Product1","Product2","Product3")))
})
output$choose_File1 <- renderUI({
selectInput("File1", "File1:", as.list(c("File1","File2","File3")))
})
# Insert the right number of plot output objects into the web page
output$plots <- renderUI({
plot_output_list <- lapply(1:input$n, …Run Code Online (Sandbox Code Playgroud) 如何从此向量中提取时间,分钟秒和毫秒的所有时间部分
d = as.POSIXct(c( "2015-09-08 17:42:07.456 GMT","2015-09-08 17:42:19.778 GMT"))
library(lubridate)
hours = hour(d)
minutes = minute(d)
sec = d...?
milliseconds = d...?
Run Code Online (Sandbox Code Playgroud)
我试图使用上面的lubridate包,但是如何将第二个和毫秒分开?
strsplit(as.character(second(d)), ".")
Run Code Online (Sandbox Code Playgroud) r ×11
asp.net-mvc ×1
c# ×1
ggplot2 ×1
knockout-2.0 ×1
knockout.js ×1
posixct ×1
r-markdown ×1
regex ×1
shiny ×1