将数据从3行重组为1 [英] Reorganizing data from 3 rows to 1
本文介绍了将数据从3行重组为1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从主要包含重复数据的csv文件重组数据。我有数据导入到数据框架中的R,但我遇到以下问题:
I need to reorganize data from a csv file that contains mostly repeating data. I have the data imported into R in a dataframe but I am having trouble with the following:
ID Language Author Keyword
12 eng Rob COLOR=Red
12 eng Rob SIZE=Large
12 eng Rob DD=1
15 eng John COLOR=Red
15 eng John SIZE=Medium
15 eng John DD=2
我需要做的是将每个关键字in a separate column
What I need to do is transform this into a row with each keyword in a separate column
ID Language Author COLOR SIZE DD
12 eng Rob Red Large 1
任何想法?
推荐答案
使用 reshape2
包很简单:
使用 tt
定义为 Gary的回答
library("reshape2")
tt <- cbind(tt, colsplit(tt$Keyword, "=", c("Name", "Value")))
tt_new <- dcast(tt, ID + Language + Author ~ Name, value.var="Value")
b $ b
它提供
which gives
> tt_new
ID Language Author COLOR DD SIZE
1 12 eng Rob Red 1 Large
2 15 eng John Red 2 Medium
这篇关于将数据从3行重组为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文