在R中重复一定的值,不同的次数

时间:2016-01-11 06:57:02

标签: r

问题

这个问题包含两部分。

首先,我有一个数据框,其中包含一些列中的数字。它看起来像这样。

//  cdst is the image
//    This is the code which i want to know what are the values in a certain pixel but the code below only detects the first then the 2nd and 3rd is equal to 0 

    void dec()
    {
        Mat em;
        cdst="path of the image";

        //this is the value of BGR per color that i wish to check
        Vec3f red(0, 0, 255);
        Vec3f blue(255, 0, 0);
        Vec3f green(0, 128, 0);
        Vec3f yellow(0, 255, 255);
        Vec3f marron(0, 0,128);
        Vec3f pink(147, 20, 255);
        Vec3f indigo(130, 0, 75);
        Vec3f midblue(112, 25, 25);
        Vec3f magenta(139, 0, 139);
        //em will hold the cdst image
        em=cdst;
        //for loop to determine what are the colors
        for (int i = 0; i < l.size(); i++)
        {
            int x = l[i][0];
            int y = l[i][1];
            cout << x << " " << y<<endl;
            Vec3f px = em.at<uchar>(y,x);

           //Im trying to print all color of the 3 channels
           //But it only the first on has a value then the second and third is 0
            cout << px.val[0] << " " << px.val[1] << " "<<px.val[2]<<endl;
            if (px == pink)
            {
                cout<<"A";
            }
        }
    }

现在,我希望根据数据框第1列中的数字重复固定值(比如文本“x”)。如果有多个重复,我希望它们用空格分隔。

所需输出

> df <- data.frame(sr.no.=c(1,2,3,4,5),num=c(1,0,2,1,0))
> View(df)
  sr.no.    num
  1         1         
  2         0
  3         2
  4         1
  5         0

我尝试使用rep函数,但它给出了一些错误,我不完全理解rep函数的工作原理。我想我知道它的作用,但不知道'怎么样'..

> View(df)
  sr.no.    num     rep
  1         1       x
  2         0       
  3         2       x x
  4         1       x
  5         0

注意:实际数据框很大,因此我们不能手动创建“x”列表。

现在,第二部分与我上面的问题无关,但它只与rep函数有关。

如果我写这个,

> rep("x",df$num)
Error in rep("x", df$num) : invalid 'times' argument
> rep("x",df[df$num])
Error: (list) object cannot be coerced to type 'integer'

我收到错误。但如果我写这个,

> rep(1:2,df$num)
Error in rep(1:2, df$num) : invalid 'times' argument

我得到了上面的输出。有人可以向我解释,首先是如何解决我的重复问题;然后,如果代表在一个案例中给出错误而另一个案件没有出现错误,那该怎么回事。

3 个答案:

答案 0 :(得分:3)

我们也可以使用data.table来做到这一点。你提到的数据很大,我怀疑很多人的数据很多。将多次出现在数据中。因此,创建每个代表&#39;只使用一次字符串并使用快速包data.table会更快。

setDT(dat)[,rep:=paste(rep("x",num),collapse=" "),by=num]
dat

> dat
   sr.no. num rep
1:      1   1   x
2:      2   0    
3:      3   2 x x
4:      4   1   x
5:      5   0    

出于兴趣,并且因为速度/性能是一个问题,我在一百万行的数据集上运行了一些基准测试。

#creating data
set.seed(123)
largedat <- data.frame(sr.no.=1:1e6, num=sample(0:50,1e5, replace=T))

这些(不足为奇)的结果。

Unit: milliseconds
     expr         min          lq        mean      median          uq         max neval cld
   heroka    20.49455    25.44678    34.85443    32.00066    44.51929    57.99193    20 a  
   Krrish   206.14323   272.43439   370.22651   377.10149   467.71401   518.09284    20  b 
 fishtank 12740.56664 12896.42906 13417.82434 13223.84195 13931.15781 14605.41123    20   c

答案 1 :(得分:2)

尝试:

> df$rep<-apply(df,1,function(x){ paste(rep("x",x["num"]),collapse=" ")})
> df
  sr.no num rep
1     1   1   x
2     2   0
3     3   2 x x
4     4   1   x
5     5   0

rep(1:2,df$num)因为df$num有5个数字而1:2只有2个数字,所以rep不知道重复1:2的次数。

rep(1:length(df$num),df$num基本上是rep(c(1,2,3,4,5),c(1,0,2,1,0))所以它知道重复1次(1次),2次(0次),3次(2次)等...

答案 2 :(得分:2)

由于您已经说过数据集非常大,因此使用apply函数可能需要一些时间来处理。 相反,你可以使用 库(stringi) 和stri_dup函数 在这种情况下,您将获得相同的输出,但速度更快。

所以使用

library(stringi)

cbind(df,rep=stri_dup("x ",df$num))

  sr.no. num  rep
1      1   1   x 
2      2   0     
3      3   2 x x 
4      4   1   x 
5      5   0     
相关问题