0

I'm quite new to the R environment and have this kind of situation:

| Sample    | Name     | Value    |
| Sample1   | ABC      | 10       |
| Sample1   | FEG      | 13       |
| Sample1   | SPQ      | 45       |
| Sample2   | ABC      |  2       |
| Sample2   | FEG      | 12       |
| Sample2   | SPQ      | 10       |
| Sample3   | ABC      | 53       |
| --------  | -------- | -------- |
| Sample10  | SPQ      | 20       |

I need to obtain this kind of dataframe:

| Name     | Sample1  | Sample2  | Sample3 | -------- | Sample10 |
| ABC      | 10       |  2       | 53      | -------- | -------- |
| FEG      | 13       | 12       | ------- | -------- | -------- |
| SPQ      | 45       | 10       | ------- | -------- | 20       |

How can I do it in the easiest way as possible? I'd like a solution also in Base R, if possible. Thank you for your help!

I tried using

df2<-data.frame(t(unique(df$Sample)))
colnames(df2)<-df2[1,]
df2$Name=NA

That gives me the "layout" of the table, but my problem is to fill the columns with the correct value. I'd rather not using a for cycle.

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138

0 Answers0