我有很多想把它们放在一页上的图,ggarrange在这方面做得很好,但是,似乎我必须将这些图中的每一个都放在列表中,它们作为此ggarrange函数的输入存储在其中,其他而不是直接将列表作为输入,请参阅以下详细信息:
一个天真的例子:
p1 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point()
p2 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point() + facet_wrap( ~ cyl, ncol=2, scales = "free") +
guides(colour="none") +
theme()
plot_list = list(p1,p2)
Run Code Online (Sandbox Code Playgroud)
我现在能做的:
ggarrange(p1,p2, widths = c(2,1), labels = c("a", "b"))
Run Code Online (Sandbox Code Playgroud)
我真正想做但没能做到的:
ggarrange(plot_list, widths = c(2,1), labels = c("a", "b"))
Run Code Online (Sandbox Code Playgroud)
有谁知道怎么做?如果地块数量很大并且可能会不时发生变化,这可以节省大量时间。样本不是我的,从这里复制的。
======== 编辑 ========
根据下面的优秀答案,至少有可用的选项:1,请参阅已接受的答案,2,来自删除的答案,我几乎没有修改
do.call(ggarrange, c(plot_list[1:2], widths = c(2, 1), labels = c("a", "b")))
将参数传递给函数ggarrange,c() …
我有一个DataFrame如下所示:
df = {'col_1': [1,2,3,4,5,6,7,8,9,10],
'col_2': [1,2,3,4,5,6,7,8,9,10],
'col_3':['A','A','A','A','A','B','B','B','B','B']}
df = pd.DataFrame(df)
Run Code Online (Sandbox Code Playgroud)
虽然我使用的真实数据有数百列,但我想使用不同的函数来操作这些列min,max 以及自定义函数,如:
def dist(x):
return max(x) - min(x)
def HHI(x):
ss = sum([s**2 for s in x])
return ss
Run Code Online (Sandbox Code Playgroud)
我想要的功能如下:
def myfunc(cols,fun):
return df.groupby('col_3')[[cols]].transform(lambda x: fun)
# which allow me to do something like:
df[['min_' + s for s in cols]] = myfunc(cols, min)
df[['max_' + s for s in cols]] = myfunc(cols, max)
df[['dist_' + s for s in cols]] = myfunc(cols, dist)
Run Code Online (Sandbox Code Playgroud)
这在Python中是否可行(我的猜测是'是')?
那如果是的话呢? …
我想要实现的很简单,在 RI 中可以做类似的事情
paste0("https\\",1:10,"whatever",11:20),
如何在 Python 中做到这一点?我在这里找到了一些东西,但只允许:
paste0("https\\",1:10).
任何人都知道如何解决这个问题,这一定很容易做到,但我找不到方法。
正如标题所述,我想对 中的分类变量进行一些汇总分析pandas,但搜索了一段时间没有找到令人满意的解决方案。因此,我开发了以下代码作为一种自我回答问题,希望有人可以帮助改进。
test_df = pd.DataFrame({'x':['a', 'b','b','c'],
'y':[1, 0, 0, np.nan],
'z':['Jay', 'Jade', 'Jia', ''],
'u':[1, 2, 3, 3]})
def cat_var_describe(input_df, var_list):
df = input_df.copy()
# dataframe to store result
res = pd.DataFrame({'var_name', 'values', 'count'})
for var in var_list:
temp_res = df[var].value_counts(dropna=False).rename_axis('unique_values').reset_index(name='counts')
temp_res['var_name'] = var
if var==var_list[0]:
res = temp_res.copy()
else:
res = pd.concat([res, temp_res], axis=0)
res = res[['var_name', 'unique_values', 'counts']]
return res
cat_des_test = cat_var_describe(test_df, ['x','y','z','u'])
cat_des_test
Run Code Online (Sandbox Code Playgroud)
任何有用的建议将不胜感激。
正如标题中所说,我有一个如下所示的 data.frame,
df<-data.frame('id'=c('1','1','1','1','1','1','1'),'time'=c('1998','2000','2001','2002','2003','2004','2007'))
df
id time
1 1 1998
2 1 2000
3 1 2001
4 1 2002
5 1 2003
6 1 2004
7 1 2007
Run Code Online (Sandbox Code Playgroud)
还有一些其他情况的时间窗口比这更短或更长,只是为了说明。
我想要做的两件事情有关这组数据,首先,找到所有那些id具有至少这里连续五年的观察,这可以通过下面的方法解决来完成此。其次,我想保持只有那些观测中的至少五个连续排的id由第一步骤中选择。理想的结果是:
df
id time
1 1 2000
2 1 2001
3 1 2002
4 1 2003
5 1 2004
Run Code Online (Sandbox Code Playgroud)
我可以使用 for 循环和diff函数编写一个复杂的函数,但是如果我有一个更大的数据集,那么这在编写函数和获取结果方面可能非常耗时,如果id。但这似乎不像 R,我相信应该有一两行解决方案。
有谁知道如何实现这一目标?您的时间和知识将不胜感激。提前致谢。