4

I'm tryng to sequence a dataset, and I'm a little lost with this. I have everything else done, data filtering, duplicated values eliminated, ordered by date... but im stuck with this, maybe one of the most simple parts. My goal is to convert this data frame:

Type    Value
A        12
B        20
A        14
A        13
B        15

To something like this:

A   12,14,13
B   20,15

Any idea on how to do this?

Thanks in advance!

Amnor
  • 380
  • 6
  • 21

4 Answers4

6

Using base is simplest:

aggregate(df$Value~df$Type,FUN=c)

  df$Type   df$Value
1       A 12, 14, 13
2       B     20, 15

using FUN=c keeps the Value type to numeric (actually a numeric vector) which is better imho than converting to String

however.... if no more transformations are needed and you want to save the above as CSV - you DO want to convert to String:

write.csv(x = aggregate(df$Value~df$Type,FUN=toString),file = "nameMe")

works fine.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Zahiro Mor
  • 1,708
  • 1
  • 16
  • 30
  • This works, but i can't write the result in a CSV document. Can it be written ina "csv like" document, or a plain text? When I try to write the csv i get this message: "Error inn .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol, : not implemented type 'list' in 'EncodeElement' – Amnor May 30 '16 at 13:37
4

We could use aggregate from base R

aggregate(Value~., df1, FUN= toString)
#   Type      Value
#1    A 12, 14, 13
#2    B     20, 15
akrun
  • 874,273
  • 37
  • 540
  • 662
2

Another Alternative using data.table:

Assumption: The data.frame is stored in variable df.

library(data.table)

setDT(df)

df[,.(Value = paste(Value,collapse=',')),.(Type)]
Kunal Puri
  • 3,419
  • 1
  • 10
  • 22
1

You could use the tidyr library.

> library(tidyr)
> spread(df, Type, Value)
   A  B
1 12 NA
2 NA 20
3 14 NA
4 13 NA
5 NA 15
Mhairi McNeill
  • 1,951
  • 11
  • 20