提取大写行,然后填充直到下一个大写行
我有一些数据,如下所示:
RegionName
<chr>
1 ANDALUCÍA
2 Almería
3 Abla
4 Abrucena
5 Adra
6 ALBÁNCHEZ
7 Alboloduy
8 Albox
9 ALCOLEA
10 Alcóntar
其中一些列是大写的。我想将大写列提取到新列中,然后填充(向下)直到下一个大写列。
预期输出:
RegionName REGIONNAME
<chr> <chr>
1 ANDALUCÍA ANDALUCÍA -first result
2 Almería ANDALUCÍA
3 Abla ANDALUCÍA
4 Abrucena ANDALUCÍA
5 Adra ANDALUCÍA
6 ALBÁNCHEZ ALBÁNCHEZ - change here
7 Alboloduy ALBÁNCHEZ
8 Albox ALBÁNCHEZ
9 ALCOLEA ALCOLEA - change here
10 Alcóntar ALCOLEA
数据:
data = structure(list(RegionName = c("ANDALUCÍA", "Almería", "Abla",
"Abrucena", "Adra", "ALBÁNCHEZ", "Alboloduy", "Albox", "ALCOLEA",
"Alcóntar")), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
"data.frame"))
I have some data which looks like:
RegionName
<chr>
1 ANDALUCÍA
2 Almería
3 Abla
4 Abrucena
5 Adra
6 ALBÁNCHEZ
7 Alboloduy
8 Albox
9 ALCOLEA
10 Alcóntar
Where some of the columns are uppercase
. I want to extract the uppercase columns into a new column and fill(down)
until the next uppercase column.
Expected output:
RegionName REGIONNAME
<chr> <chr>
1 ANDALUCÍA ANDALUCÍA -first result
2 Almería ANDALUCÍA
3 Abla ANDALUCÍA
4 Abrucena ANDALUCÍA
5 Adra ANDALUCÍA
6 ALBÁNCHEZ ALBÁNCHEZ - change here
7 Alboloduy ALBÁNCHEZ
8 Albox ALBÁNCHEZ
9 ALCOLEA ALCOLEA - change here
10 Alcóntar ALCOLEA
Data:
data = structure(list(RegionName = c("ANDALUCÍA", "Almería", "Abla",
"Abrucena", "Adra", "ALBÁNCHEZ", "Alboloduy", "Albox", "ALCOLEA",
"Alcóntar")), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
"data.frame"))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个想法是使用
grepl()
识别[[:upper:]]
,将其他转换为 NA 和fill()
, IEAn idea is to use
grepl()
to recognise the[[:upper:]]
, convert the others to NA andfill()
, i.e.您可以根据区域名称是否为大写字母
==
将区域分组在一起。然后将组内的所有名称设置为全部大写的first
RegionName
。输出
数据
You can group the regions together based on if their name is
==
to their name in all upper case. Then set all names within the group to thefirst
RegionName
which is in all caps.Output
Data
ifelse
和填充
的替代方案:An alternative with
ifelse
andfill
: