V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17
1 1 1 round-0 10523 180 yellow NA NA NA NA NA NA NA NA
2 11973 1 round-1 19478 150 yellow NA NA NA NA NA NA NA NA
3 22428 1 round-2 28928 130 yellow 29928 150 brake 31433 160 red NA NA NA NA
4 39333 1 round-3 47333 160 yellow 48588 185 red NA NA NA NA NA NA
5 49788 1 round-4 56288 130 yellow 58038 165 brake 59038 175 red NA NA NA NA
6 64688 1 round-5 71693 140 yellow 74293 192 red 74393 194 crash NA NA NA NA
7 85148 1 round-6 91648 130 yellow 94648 190 red NA NA NA NA NA NA
8 95598 1 round-7 103653 130 yellow 104903 155 brake 105403 165 red NA NA NA NA
9 112703 1 round-8 122758 130 yellow 125758 190 red 125758 190 crash NA NA NA NA
10 136513 1 round-9 146563 130 yellow 147963 158 brake 148063 160 red NA NA NA NA
11 157118 1 round-10 164618 150 yellow 167118 200 red NA NA NA NA NA NA
12 167568 1 round-11 179123 120 yellow 182023 178 red 182373 185 brake 182623 190 crash NA NA
13 193378 1 round-12 200378 140 yellow 201878 170 red 203278 198 crash NA NA NA NA
所以我在csv中有一个表,用read.table导入,如上所示。正如您所看到的,每行中的数据都不均匀。它以三联体的形式存在,V3 V6等存储字符串,它们与存储在它们前面的两个单元格中的数字相对应
所以困扰我的是,我似乎无法弄清楚如何编写一个计算表中模式数量的计数。我的任务是计算碰撞出现右刹车的次数。我知道我可以像嵌套的ifelse一样使用,比如我用来存储找出制动前两个单元格的数字:
df$brake <- ifelse(df$V9 == "brake", df$V7,
ifelse(df$V12 == "brake", df$V10,
ifelse(df$V15 == "brake", df$V13,
ifelse(df$V18 == "brake", df$V16,
ifelse(df$V21 == "brake", df$V19,
ifelse(df$V24 == "brake", df$V22,
ifelse(df$V27 == "brake", df$V25, NA)))))))
,但我有35个专栏,我只是想知道是否有更精简的方法。任何帮助将不胜感激:)
编辑:对不起,目前还不清楚。我遇到的主要问题是如何计算紧接着刹车后的刹车频率。 (我添加了用于在制动之前提取数值2个单元格的代码,因为我认为两者的解决方案都是相似的:基本上可以看到在任何制动器之后3个单元是否崩溃,而另一个是否可以看到在制动前提取两个单元格的值。抱歉混淆)答案 0 :(得分:0)
根据显示的代码,我假设&#39;刹车&#39;列是9,12,15等。因此,我们创建一个数字索引(&#39; indx&#39;)来提取这些列。还为“制动”创建了逻辑矩阵。列(&#39; indx1&#39;)。然后,我们可以创建行索引(1:nrow(df)
)和列索引(max.col(indx1, 'first')
),cbind
,并提取属于7, 10, 13, etc.
中列的元素。我们将元素更改为NA
,对应于&lt; indx1&#39;中的行。这是&#39; 0&#39; 0对于rowSums
indx <- seq(9,ncol(df), by=3)
indx1 <- df[indx]=='brake'
df$brake <- df[indx-2][cbind(1:nrow(df), max.col(indx1, 'first'))]*
NA^!rowSums(indx1)
df$brake
#[1] NA NA 29928 NA 58038 NA NA 104903 NA 147963
#[11] NA 182373 NA
df <- structure(list(V1 = c(1L, 11973L, 22428L, 39333L, 49788L, 64688L,
85148L, 95598L, 112703L, 136513L, 157118L, 167568L, 193378L),
V2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), V3 = c("round-0", "round-1", "round-2", "round-3", "round-4",
"round-5", "round-6", "round-7", "round-8", "round-9", "round-10",
"round-11", "round-12"), V4 = c(10523L, 19478L, 28928L, 47333L,
56288L, 71693L, 91648L, 103653L, 122758L, 146563L, 164618L,
179123L, 200378L), V5 = c(180L, 150L, 130L, 160L, 130L, 140L,
130L, 130L, 130L, 130L, 150L, 120L, 140L), V6 = c("yellow",
"yellow", "yellow", "yellow", "yellow", "yellow", "yellow",
"yellow", "yellow", "yellow", "yellow", "yellow", "yellow"
), V7 = c(NA, NA, 29928L, 48588L, 58038L, 74293L, 94648L,
104903L, 125758L, 147963L, 167118L, 182023L, 201878L), V8 = c(NA,
NA, 150L, 185L, 165L, 192L, 190L, 155L, 190L, 158L, 200L,
178L, 170L), V9 = c("", "", "brake", "red", "brake", "red",
"red", "brake", "red", "brake", "red", "red", "red"), V10 = c(NA,
NA, 31433L, NA, 59038L, 74393L, NA, 105403L, 125758L, 148063L,
NA, 182373L, 203278L), V11 = c(NA, NA, 160L, NA, 175L, 194L,
NA, 165L, 190L, 160L, NA, 185L, 198L), V12 = c("", "", "red",
"", "red", "crash", "", "red", "crash", "red", "", "brake",
"crash"), V13 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 182623L, NA), V14 = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 190L, NA), V15 = c("", "", "", "", "", "", "",
"", "", "", "", "crash", ""), V16 = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA), V17 = c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("V1", "V2",
"V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12",
"V13", "V14", "V15", "V16", "V17"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"
))