我有一个看起来像这样的表:
Character Legend Example Sample Match
\t Tab T\t\w{2} T ab
\r Carriage return character see below
\n Line feed character see below
\r\n Line separator on Windows AB\r\nCD AB
CD
\N Perl, PCRE (C, PHP, R…): one character that is not a line break \N+ ABC
\h Perl, PCRE (C, PHP, R…), Java: one horizontal whitespace character: tab or Unicode space separator
\H One character that is not a horizontal whitespace
\v .NET, JavaScript, Python, Ruby: vertical tab
\v Perl, PCRE (C, PHP, R…), Java: one vertical whitespace character: line feed, carriage return, vertical tab, form feed, paragraph or line separator
\V Perl, PCRE (C, PHP, R…), Java: any character that is not a vertical whitespace
\R Perl, PCRE (C, PHP, R…), Java: one line break (carriage return + line feed pair, and all the characters matched by \v)
(“天”列不涉及我的偏度和峰度计算,仅在我的表中)
我想要一个表,用于按年份分组计算偏度和峰度值:
start_table <- data.frame("Water_Year" = c("1903", "1903", "1904", "1904"), "X" = c(13, 11, 12,
15), "Day" = c(1, 2, 1, 2))
我不知道如何按年份将其分组以执行这些计算。
答案 0 :(得分:2)
将fBasics
与data.table
一起使用:
library(fBasics)
library(data.table)
setDT(start_table)[, .(Skew = skewness(X), Kurtosis=kurtosis(X)), .(Water_Year)][]
#> Water_Year Skew Kurtosis
#> 1: 1903 0 -2.75
#> 2: 1904 0 -2.75
答案 1 :(得分:2)
您还可以定义偏度/峰度函数:
kurtosis <- function(x) {
m4 <- mean((x - mean(x))^4)
kurtosis <- m4/(sd(x)^4) - 3
kurtosis
}
skewness <- function(x) {
m3 <- mean((x - mean(x))^3)
skewness <- m3/(sd(x)^3)
skewness
}
然后,将其应用于base R
:
aggregate(X ~ Water_Year,
FUN = function(x) c(kurtosis = kurtosis(x), skewness = skewness(x)),
data = start_table)
Water_Year X.kurtosis X.skewness
1 1903 -2.75 0.00
2 1904 -2.75 0.00
答案 2 :(得分:1)
一个选项是# Load Pyomo
from pyomo.environ import *
import numpy as np
# Create model
m3 = ConcreteModel()
## Declare variables with initial values WITHOUT bounds
m3.x1 = Var(initialize=1)
m3.x2 = Var(initialize=1)
## Declare objective
m3.OBJ = Objective(expr=m3.x1**3 - m3.x2 - m3.x1*m3.x2 - m3.x2**2, sense = minimize)
## Declare equality constraints
m3.h = Constraint(expr= m3.x1**2 + m3.x2**2 == 1)
# initial near global min
theta0 = 1.0
# initialize near local min
# theta0 = np.pi
m3.x1 = np.sin(theta0)
m3.x2 = np.cos(theta0)
# Specify solver
solver=SolverFactory('baron')
solver.options['LocRes'] = 1
solver.options['results'] = 1
# Solve model
solver.solve(m3, tee=True)
## Return the solution
print("x1 = ",value(m3.x1))
print("x2 = ",value(m3.x2))
print("objective = ",value(m3.OBJ))
print("\n")
res.lst