如何选择a的前4行data.frame
:
Weight Response
1 Control 59 0.0
2 Treatment 90 0.8
3 Treatment 47 0.1
4 Treamment 106 0.1
5 Control 85 0.7
6 Treatment 73 0.6
7 Control 61 0.2
Run Code Online (Sandbox Code Playgroud) 我想每次都重复data.frame的行N
.结果应该是一个新的data.frame
(with nrow(new.df) == nrow(old.df) * N
)保持列的数据类型.
N = 2的示例:
A B C
A B C 1 j i 100
1 j i 100 --> 2 j i 100
2 K P 101 3 K P 101
4 K P 101
Run Code Online (Sandbox Code Playgroud)
因此,每行重复2次,字符仍然是字符,因素仍然是因素,数字仍然是数字,...
我的第一次尝试使用了:apply(old.df, 2, function(co) rep(co, each = N))
,但是这个将我的值转换为字符,我得到:
A B C
[1,] "j" "i" "100"
[2,] "j" "i" "100"
[3,] "K" "P" "101"
[4,] "K" "P" "101"
Run Code Online (Sandbox Code Playgroud) 这是我的(PostgreSQL)表 -
test=> create table people (name varchar primary key,
marriage_status varchar) ;
test=> insert into people values ('Ken', 'married');
test=> insert into people values ('May', 'single');
test=> insert into people values ('Joe', NULL);
Run Code Online (Sandbox Code Playgroud)
我想选择被所有的人不知道要结婚了,即包括那些NULL marriage_status.
这并不能正常工作-
test=> select * from people where marriage_status != 'married' ;
name | marriage_status
------+-----------------
May | single
(1 row)
Run Code Online (Sandbox Code Playgroud)
当然这样做 -
test=> select * from people where marriage_status != 'married'
or marriage_status is NULL ;
name | marriage_status
------+----------------- …
Run Code Online (Sandbox Code Playgroud) 是否可以使用argparse
捕获任意一组可选参数?
例如,以下两者都应被接受为输入:
python script.py required_arg1 --var1 value1 --var2 value2 --var3 value3
python script.py required_arg1 --varA valueA --var2 value2 --varB valueB
Run Code Online (Sandbox Code Playgroud)
先验我不知道将指定哪些可选参数接收但会相应地处理它们.
它看起来像是pd.rolling_mean
被弃用了ndarrays
,
Run Code Online (Sandbox Code Playgroud)pd.rolling_mean(x, window=2, center=False)
FutureWarning:对于ndarrays,不推荐使用pd.rolling_mean,将来的版本将删除
但根据这个SO答案,它似乎是最快的方式.
现在有没有新的方法直接使用SciPy或NumPy这样做pd.rolling_mean
?
在ggplot2
,使用跨越行和列的构面创建刻面图很容易.有没有"光滑"的方式来做到这一点altair
?facet
文件
可以在一列中绘制构面图,
import altair as alt
from vega_datasets import data
iris = data.iris
chart = alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N'
).properties(
width=180,
height=180
).facet(
row='species:N'
)
Run Code Online (Sandbox Code Playgroud)
在一排,
chart = alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N'
).properties(
width=180,
height=180
).facet(
column='species:N'
)
Run Code Online (Sandbox Code Playgroud)
但通常情况下,我只想使用多个列/行在网格中绘制它们,即在单个列/行中排列的那些并不意味着任何特定的含义.
例如,看到facet_wrap
来自ggplot2
:http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/#facetwrap
我一直在做一些决策树情节,如下所示
library(party)
irisct <- ctree(Species ~ ., data = iris)
plot(irisct, type="simple")
Run Code Online (Sandbox Code Playgroud)
但现在我想看到图像的基础64,所以我可以将图形保存为JSON.
或者是否有另一种方式将R图像发送到网络?
我想检查文件是否存在,如果不存在,则创建它.
考虑以下C代码的等价物,但使用shell脚本.
if(!exist) {
command;
command;
}
else {
command;
command;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用annotate()
我的一个ggplot2
图上覆盖文字.我正在使用该选项,parse=T
因为我需要使用希腊字母rho.我想要文字说= -0.50
,但是尾随的零被剪掉了,而我得到了-0.5
.
这是一个例子:
library(ggplot2)
x<-rnorm(50)
y<-rnorm(50)
df<-data.frame(x,y)
ggplot(data=df,aes(x=x,y=y))+
geom_point()+
annotate(geom="text",x=1,y=1,label="rho==-0.50",parse=T)
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么能得到最后的0出现?我以为我可以这样使用paste()
:
annotate(geom="text",x=1,y=1,label=paste("rho==-0.5","0",sep=""),parse=T)
Run Code Online (Sandbox Code Playgroud)
但后来我得到了错误:
Error in parse(text = lab) : <text>:1:11: unexpected numeric constant
1: rho==-0.5 0
^
Run Code Online (Sandbox Code Playgroud)