-1

This may be a very basic question but I am not able to fo since last hour. I want to merge cells in each row using a comma or semicolon. The data looks like

OTU_1   23  15  273 51  127 190 220 83  k__Bacteria p__Chloroflexi  c__SJA-15   o__C10_SB1A f__C10_SB1A g__Candidatus Amarilinum    s__

The output would be like this

OTU_1;23;15;273;51;127;190;220;83;k__Bacteria;p__Chloroflexi;c__SJA-15;o__C10_SB1A;f__C10_SB1A;g__Candidatus Amarilinum;s__

Can you please guide how it can be done in R. I know how to use concatinate function but i am wondering if it can be done in R?

Thanks

Muhammad Arslan
  • 35
  • 1
  • 1
  • 5
  • 1
    Do you want something like `paste(1:5, collapse=";")` Hard to tell exactly how your data is structured. Make sure to share data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Aug 08 '18 at 21:35
  • Possible duplicate of [Is there a way to paste together the elements of a vector in R without using a loop?](https://stackoverflow.com/questions/6698839/is-there-a-way-to-paste-together-the-elements-of-a-vector-in-r-without-using-a-l) – divibisan Aug 08 '18 at 21:39
  • Please give a reproducible example. You should think about what the input is. How are columns separated? Is there a header? What is the output (you have that). How should I write it, including a header, if present, or not? – Roman Luštrik Aug 09 '18 at 12:12

2 Answers2

0

It's not clear what you mean by cells. Is this a vector? Columns of a data.frame?

Tool to use here might be paste, but how you use it might depend on what the underlying structure of the data is.

> paste(letters, collapse = ";")
[1] "a;b;c;d;e;f;g;h;i;j;k;l;m;n;o;p;q;r;s;t;u;v;w;x;y;z"
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • This is .csv file. I simply imported .csv file in R and now want to merge them all in one cell. and them export again as .csv file. And I have hundreds of thousands of rows and a few hundred columns – Muhammad Arslan Aug 08 '18 at 21:38
  • @MuhammadArslan so basically you want to change the delimiter which separates columns? Can you provide a few rows of one file and how you import it? – Roman Luštrik Aug 09 '18 at 12:09
0

to merge cells by rows you can use apply with second argument equal to 1, i.e.

apply(your_dataframe,1, function(x) paste(x, collapse = ","))

You'll receive a list of length equal to number of rows where each element is equal to cell merge

Nar
  • 648
  • 4
  • 8