R:将多行重组为一行(按第一列中的值重新分配)

时间:2017-06-22 20:25:38

标签: r

我在R中导入的CSV中有一个数据框,例如:

Salmon  6
Salmon  4
Salmon  7
Trout   2
Trout   3
Trout   2

我想重新安排:

Salmon  6  4  7

Trout  2  3  2

转置值分别位于新列中。

2 个答案:

答案 0 :(得分:2)

您可以使用aggregate

aggregate(V2 ~ V1, data = mydata, c)

#       V1 V2.1 V2.2 V2.3
# 1 Salmon    6    4    7
# 2  Trout    2    3    2

<强> 数据:

read.table(text='Salmon  6
Salmon  4
Salmon  7
Trout   2
Trout   3
Trout   2', header=F, quote='"') -> 
mydata 

答案 1 :(得分:1)

以下是使用 public async Task<double> CalculateAverageAge() { var client = new MobileServiceClient(AppUrl, AppKey); var table = client.GetTable<Person>(); var sum = 0.0; var count = 0; var items = await table.Take(10).ToEnumerableAsync(); while (items != null && items.Count() != 0) { count += items.Count(); sum += Enumerable.Sum(items, i => i.Age); var queryResult = items as IQueryResultEnumerable<Person>; if (queryResult != null && queryResult.NextLink != null) { items = await table.ReadAsync<Person>(queryResult.NextLink); } else { items = null; } } return sum / count; }

的选项
dcast

数据

library(data.table)
dcast(setDT(mydata), V1~paste0("VN", rowid(V1)), value.var = 'V2')
#       V1 VN1 VN2 VN3
#1: Salmon   6   4   7
#2:  Trout   2   3   2