在每行中拆分一个字符串并复制该行

时间:2015-10-31 00:54:46

标签: r

在我正在处理的数据框中,有一列字符串可能包含&#34 ;;"。我想找到它们并拆分由";"分隔的字符串。并复制该行并将分隔的字符串放入不同的行。

以下是数据框的示例:

name     value
a        10
b;c      20
d        30
e        40
f;g;h    50

这就是我想要的:

name     value
a        10
b        20
c        20
d        30
e        40
f        50
g        50
h        50

这是我试图写的:

  DF$name <- sapply(DF$name,function(x) {
     if (grepl(";",DF$name)){
     unlist(strsplit(DF$name,"[;]"))}})

错误消息msg说:

  the condition has length > 1 and only the first element will be used

我也不知道如何将拆分字符串放入不同的行

1 个答案:

答案 0 :(得分:3)

#include<stdio.h>
#include<stdlib.h>

  int main(void)
  {

    //Fields
    int i=0, j=0, k=0, l=0, num = 0, x = 0, count = 0, total = 0, z = 0;
    signed int b[11] = {0};
    char discard;


    //Prompt message
    printf( "\n\nPlease enter your list of numbers: " );


    //This while loop scans until the enter button is pressed
    while(i < 11 && (scanf("%d%1[^\n]s", &b[i], &discard)) == 2)
    {
      ++count;
      i++;
    } 
    puts("");
    puts("");
    //Display Factors 

    while(k <= count)
    { 
      x=b[k];
      num = sum_divisors(x);
      printf("%d " , num);
      k++;  
      puts("");
    }


   }//End of main

    //function to sum the divisors together
    int sum_divisors(int a)
    {
      int total = 0;
      int z = 0;

      printf("%d ", a);

      if(a < 1)
      { 
         total = 0;
      }else
      {     
         if((a%1 == 0) && a !=1)
              total = total + 1;

         for(z=2; z<102; z++)
         {
           if((a%z == 0) && a != z)
           {
             total = total + z;
           }//if
         }//for
      }//end if/else statement
      //  printf("%d ", total); 
      return total;
    }//end function sum_divisors