Imagine a data frame...
df <- rbind("title", "------------------", "quite long content", "quite long content", "------------------", "short cont", "short cont")
df <- as.data.frame(df)
df
...which prints:
> df
V1
1 title
2 ------------------
3 quite long content
4 quite long content
5 ------------------
6 short cont
7 short cont
I would like to add spaces to all elements below the last line (they can vary in number) so they align with elements in the other lines, such that:
> df
V1
1 title
2 ------------------
3 quite long content
4 quite long content
5 ------------------
6 short cont
7 short cont
Could someone help me with a way to achieve this? Many thanks in advance.
CodePudding user response:
after_last_line <- !rev(cumany(grepl("^- $", rev(df$V1))))
after_last_line
# [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE
df$V1[after_last_line] <- paste0(df$V1[after_last_line], " ")
df
# V1
# 1 title
# 2 ------------------
# 3 quite long content
# 4 quite long content
# 5 ------------------
# 6 short cont
# 7 short cont