在PostgreSQL中,我想使用SQL语句组合两列并从中创建一个新列.
我正在考虑使用concat(...),但有更好的方法吗?
最好的方法是什么?
使用数据框:
df <- data.frame(id = rep(1:3, each = 5)
, hour = rep(1:5, 3)
, value = sample(1:15))
Run Code Online (Sandbox Code Playgroud)
我想添加一个匹配的累积和列id:
df
id hour value csum
1 1 1 7 7
2 1 2 9 16
3 1 3 15 31
4 1 4 11 42
5 1 5 14 56
6 2 1 10 10
7 2 2 2 12
8 2 3 5 17
9 2 4 6 23
10 2 5 4 27
11 3 1 1 …Run Code Online (Sandbox Code Playgroud) 同
df <- data.frame(id=c(1:5), v1=c(0,15,9,12,7), v2=c(9,32,6,17,11))
Run Code Online (Sandbox Code Playgroud)
如何在大于10的所有列上提取具有值的行,该列应返回:
id v1 v2
2 2 15 32
4 4 12 17
Run Code Online (Sandbox Code Playgroud)
如果在任何大于10的列上,该怎么办?
id v1 v2
2 2 15 32
4 4 12 17
5 5 7 11
Run Code Online (Sandbox Code Playgroud) 我有以下格式的Feed:
Hour Key ID Value
1 K1 001 3
1 K1 002 2
2 K1 005 4
1 K2 002 1
2 K2 003 5
2 K2 004 6
Run Code Online (Sandbox Code Playgroud)
并且我希望将Feed分组(Hour, Key)然后将其加总,Value但保持ID为元组:
({1, K1}, {001, 002}, 5)
({2, K1}, {005}, 4)
({1, K2}, {002}, 1)
({2, K2}, {003, 004}, 11)
Run Code Online (Sandbox Code Playgroud)
我知道如何使用FLATTEN生成总和Value但不知道如何输出ID为元组.这是我到目前为止:
A = LOAD 'data' AS (Hour:chararray, Key:chararray, ID:chararray, Value:int);
B = GROUP A BY (Hour, Key);
C = …Run Code Online (Sandbox Code Playgroud) 随着df:
df <- data.frame(value=abs(rnorm(100, 25, 5)), status=sample(0:1,100,replace=T))
df$value[sample(1:100,5)] <- NA
Run Code Online (Sandbox Code Playgroud)
我需要得到一个频率(百分比)表(更好地返回一个矩阵),如下所示:
value | status(0) status(1)
----------------------------
<=25 | 23 (23%) 20 (20%)
>25 | 27 (27%) 25 (25%)
NA | 3 (3%) 2 (2%)
Run Code Online (Sandbox Code Playgroud)
我可以这样做:
br <- seq(0, 50, 25)
with(df, summary(cut(value[status==0], br, labels=br[-1],
include.lowest=T, ordered_result=T)))
with(df, summary(cut(value[status==1], br, labels=br[-1],
include.lowest=T, ordered_result=T)))
Run Code Online (Sandbox Code Playgroud)
但是会有一次性的方式返回上面的矩阵吗?谢谢!
例如,我有一个表存储值:
select * from myvalue;
val
-------
12345
(1 row)
Run Code Online (Sandbox Code Playgroud)
如何将其保存12345到postgresql或shell脚本中的变量中?
这是我在shell脚本中尝试的内容:
var=$(psql -h host -U user -d db <<SQLSTMT
SELECT * FROM myvalue;
SQLSTMT)
Run Code Online (Sandbox Code Playgroud)
但是echo $var给了我:
val ------- 12345 (1 row)
Run Code Online (Sandbox Code Playgroud)
我也试过了
\set var (select * from myvalue)
Run Code Online (Sandbox Code Playgroud)
在psql中,当我键入\set它时列出:
var = '(select*frommyvalue)'
Run Code Online (Sandbox Code Playgroud) 这实际上与我的其他问题相对应,但事情变得更加复杂.我有data.frame和vector:
df <- data.frame(key=c(0,3:6), value=c(0,52,26,12,1))
x <- c(3,4,3,3,5,5,6,6,6,6)
Run Code Online (Sandbox Code Playgroud)
并且需要从基于x的df获取值作为键:
[1] 52 26 52 52 12 12 1 1 1 1
Run Code Online (Sandbox Code Playgroud)
上一个答案的解决方案只能给出没有重复的结果:
df[df$key %in% x,"value"]
[1] 52 26 12 1
Run Code Online (Sandbox Code Playgroud)
有办法解决这个问题吗?
另外,我看到hash()可以执行以下操作:
h <- hash( keys=letters, values=1:26 )
h$a # 1
h[ "a" ]
h[[ "a" ]]
z <- rep(letters[3:5],2)
h[z] # still with NO duplicates
<hash> containing 3 key-value pair(s).
c : 3
d : 4
e : 5
Run Code Online (Sandbox Code Playgroud)
但似乎它无法返回值的向量,例如:
h[[z]]
Error in h[[z]] : …Run Code Online (Sandbox Code Playgroud) 有没有办法从另一个文件中获取模式(模式列表)ack作为-f选项grep?我看到有一个-f选项,ack但它与-fin 有所不同grep.
也许一个例子会给你一个更好的主意.假设我有file1:
file1:
a
c
e
Run Code Online (Sandbox Code Playgroud)
和file2:
file2:
a 1
b 2
c 3
d 4
e 5
Run Code Online (Sandbox Code Playgroud)
我想从file2获取file1中的所有模式,以便:
a 1
c 3
e 5
Run Code Online (Sandbox Code Playgroud)
可以ack这样吗?否则,是否有更好的方法来处理作业(比如awk使用哈希),因为我在这两个文件中都有数百万条记录,真的需要一种有效的方法来完成?谢谢!
我有两个具有不同名称和值集的字符向量:
x <- c("a", "b", "c", "d", "e")
names(x) <- c("foo", "bar", "baz", "qux", "grault")
y <- c("c", "a", "d", "b")
names(y) <- c("bar", "foo", "qux", "corge")
Run Code Online (Sandbox Code Playgroud)
有没有办法比较x,y所以我们知道它们对应于名称的值bar是不同的,因为这里x.bar = "b"和y.bar = "c"?请注意,这些名字并未订购.我试过setdiff,which(x != y)但没有人给我正确的答案.谢谢!
我知道expand.grid是创建给定矢量的所有组合.但是有没有办法通过将数据帧中的每一行视为唯一来生成数据帧和向量的所有组合.例如,
df <- data.frame(a = 1:3, b = 5:7)
c <- 9:10
Run Code Online (Sandbox Code Playgroud)
如何创建一个新的数据框架,它是扩展df和c不扩展的组合df:
df.c:
a b c
1 5 9
2 6 9
3 7 9
1 5 10
2 6 10
3 7 10
Run Code Online (Sandbox Code Playgroud)
谢谢!