我有位置数据。我想通过先前的值(没有前导空格)来连接值(具有前导空格)。
mydata <- read.table(header = T, text = '
Locations Asset Price
"La Rioja" "Commercial premises : Rental" 386
"La Rioja" "Industrial building / warehouse : Rental" 62
" Logroño " "Offices" 103
" Logroño " "Land" 45
" Logroño " "Storage rooms" 8
" Madr " "Offices" 103
" Madr " "Land" 45
" Madr " "Storage rooms" 8
"Las Palmas" "Offices" 237
"Las Palmas" "Land" 2277
"Las Palmas" "Storage rooms" 104
"Madrid" "Industrial building / warehouse : Rental" 1839
" Pozuelo de Alarcón " "Offices" 20
')
我正在寻找下面绿色突出显示的列。红色是我的输入列。
这是不使用的另tidyverse
一种方式zoo
。
library(tidyverse)
mydata %>%
mutate(prefix = case_when(str_sub(Locations, 1, 1) == " " ~ NA_character_,
TRUE ~ Locations)) %>%
fill(prefix, .direction = "down") %>%
mutate(New_Location = case_when(prefix == Locations ~ prefix,
TRUE ~ paste(prefix, Locations, sep = ","))) %>%
select(-prefix)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句