如果我有这样的表:
| FileName | Category| Value | Number |
|:--------:|:-------:|:-----:|:------:|
| File1 | Time | 123 | 1 |
| File1 | Size | 456 | 1 |
| File1 | Final | 789 | 1 |
| File2 | Time | 312 | 2 |
| File2 | Size | 645 | 2 |
| File2 | Final | 978 | 2 |
| File3 | Time | 741 | 1 |
| File3 | Size | 852 | 1 |
| File3 | Final | 963 | 1 |
| File1 | Time | 369 | 2 |
| File1 | Size | 258 | 2 |
| File1 | Final | 147 | 2 |
| File3 | Time | 741 | 2 |
| File3 | Size | 734 | 2 |
| File3 | Final | 942 | 2 |
| File1 | Time | 997 | 3 |
| File1 | Size | 245 | 3 |
| File1 | Final | 985 | 3 |
| File2 | Time | 645 | 3 |
| File2 | Size | 285 | 3 |
| File2 | Final | 735 | 3 |
| File3 | Time | 198 | 3 |
| File3 | Size | 165 | 3 |
| File3 | Final | 753 | 3 |
我可以在R脚本中使用什么方法为Value
最小和{{1}的每个FileName
声明一个Number
变量是Category
?
( 编辑: 应该注意 {中有 Time
个条目{1}} 列。因此,应构建此代码以将空条目视为不存在,因此 null
最终不会被填充 Value
值。)
然后我想New Column
这个在现有表格上形成一个新列,现在看起来像这样:
NA
答案 0 :(得分:3)
使用data.table:
(编辑反映@ Frank的评论)
DT[, Benchmark := Value[Category == "Time"][which.min(Number[Category == "Time"])], by = FileName]
打破这种局面:
Number[Category == "Time"]
Number
Category == Time
which.min(^^^)
Benchmark := Value[Category == "Time"][^^^]
by = FileName
答案 1 :(得分:0)
未经测试,但应该让你开始:
Ref <- Table1 %>%
mutate(Category2 = factor(Category, c("Time", "Size", "Final"),
FileNumber = as.numeric(sub("File", "", FileName)),
FilePrefix = "File") %>%
arrange(FilePrefix, FileNumber, Category2, Value) %>%
group_by(FilePrefix, FileNumber, Category2) %>%
mutate(NewColumn = Value[1])