我有一个这样的数据框:
tonelabel <- c("H*", "L-", "H*", "L-%", "(L+H*)", "(L*+!H)", "!H*", "H-", "L*", "H-%", "(H*)", "^H*", "H-", "H*", "H-%", "H*", "H-%", "L*", "H-%", "H*", "L-")
boundary <- c("none", "low", "none", "low", "none", "none", "none", "high", "none", "high", "none", "none", "high", "none", "high", "none", "high", "none", "high", "none", "low")
df <- data.frame(tonelabel, boundary)
df
tonelabel boundary
1 H* none
2 L- low
3 H* none
4 L-% low
5 (L+H*) none
6 (L*+!H) none
7 !H* none
8 H- high
9 L* none
10 H-% high
11 (H*) none
12 ^H* none
13 H- high
14 H* none
15 H-% high
16 H* none
17 H-% high
18 L* none
19 H-% high
20 H* none
21 L- low
在df $ boundary列中,案例名为&#34; none&#34;必须被&#34; low&#34;或&#34;高&#34;。决定是否应该&#34;低&#34;或高#34;必须基于以下&#34;低&#34;或高#34;。所以,如果接下来的事情是&#34; none&#34;是一个&#34;低&#34;然后所有前面的&#34;无&#34;应该成为&#34;低&#34;。如果下一件事是&#34;高&#34;,则所有前面的&#34;无&#34;应该成为&#34;高&#34;。一直到下一个&#34;低&#34;或&#34;高&#34;,即。
以下是我希望输出的样子:
tonelabel boundary
1 H* low
2 L- low
3 H* low
4 L-% low
5 (L+H*) high
6 (L*+!H) high
7 !H* high
8 H- high
9 L* high
10 H-% high
11 (H*) high
12 ^H* high
13 H- high
14 H* high
15 H-% high
16 H* high
17 H-% high
18 L* high
19 H-% high
20 H* low
21 L- low
我无法将任何解决方案视为定义什么&#34; none&#34;必须遵循&#34; none&#34;,而不是在它之前。实际上,人们必须自下而上地工作。任何想法都将非常感谢!!
答案 0 :(得分:3)
使用zoo
包的解决方案
df$boundary[df$boundary == 'none'] <- NA
df$boundary <- zoo::na.locf(df$boundary, fromLast = TRUE)
或通过tidyverse
,
library(tidyverse)
df <- df %>%
mutate(boundary = replace(boundary, boundary == 'none', NA)) %>%
fill(boundary, .direction = 'up')
两者都给予,
tonelabel boundary 1 H* low 2 L- low 3 H* low 4 L-% low 5 (L+H*) high 6 (L*+!H) high 7 !H* high 8 H- high 9 L* high 10 H-% high 11 (H*) high 12 ^H* high 13 H- high 14 H* high 15 H-% high 16 H* high 17 H-% high 18 L* high 19 H-% high 20 H* low 21 L- low