Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-Studio中的条件_R_If Statement_Sampling - Fatal编程技术网

R-Studio中的条件

R-Studio中的条件,r,if-statement,sampling,R,If Statement,Sampling,我有这个问题陈述,我举个例子: Product_id product_type views inventory 1 producttype1 Y Y 2 producttype2 N N 3 producttype3 Y Y 4 producttype4 N N 5 producttype5 Y Y 6 producttype6 N N 7 producttype7 Y Y 8 pr

我有这个问题陈述,我举个例子:

Product_id  product_type    views   inventory
1   producttype1    Y   Y
2   producttype2    N   N
3   producttype3    Y   Y
4   producttype4    N   N
5   producttype5    Y   Y
6   producttype6    N   N
7   producttype7    Y   Y
8   producttype1    N   N
9   producttype2    Y   Y
10  producttype3    N   N
11  producttype4    Y   Y
12  producttype5    N   N
13  producttype6    Y   Y
14  producttype7    N   N
15  producttype7    Y   Y
我有1000万人口,我试图从中提取10%的人口样本,我必须按产品类型、视图对他们进行分组。但最终当我得到样本时,如果样本小于500k,那么我可以保持原样,但在样本最高超过500k的情况下,我必须将样本减少到500k。这是我写给group并提取10%样本的代码:

MPSSAMPLE %>% 
  group_by(product_type, views) %>%
  sample_frac(.10) -> sampledData

有人能帮我解决这些问题吗?

你可以使用
min
来获得最少500k或该组样本总数的10%

library(dplyr)

n <- 500000

MPSSAMPLE %>% 
  group_by(product_type, views) %>%
  sample_n(min(n() * 0.1, n)) -> sampledData
库(dplyr)
n%
分组依据(产品类型、视图)%>%
样本n(最小值(n()*0.1,n))->样本数据

当问题与使用R的任何上下文无关时,为什么标题中会显示“R-Studio”?我用R来表示这个问题陈述:D