Вставить строки до и после условия в data.frame - PullRequest
0 голосов
/ 02 июня 2018

У меня есть такие данные:

df <- data.frame(V1=c("stuff", "2nd June 2018", "otherstuff1", "baseball","", "142", "otherstuff2", "football","", "150", "4th June 2018", "otherstuff99", "hockey","", "160", "otherstuff100", "baseball", "", "190", "otherstuff5", "lacrosse", "200", "9th June 2018"), stringsAsFactors = F)

Я хочу вставить строку по условию, новые ячейки «дата» в форзацах с любым значением даты.Между датами есть случайные числа ячеек другого материала:

df.desired <- data.frame(V1=c("stuff","date", "2nd June 2018","date" ,"otherstuff1", "baseball","", "142", "otherstuff2", "football","", "150","date", "4th June 2018","date", "otherstuff99", "hockey","", "160", "otherstuff100", "baseball", "", "190", "otherstuff5", "lacrosse", "200", "date", "9th June 2018","date"), stringsAsFactors=F)                 

Ответы [ 2 ]

0 голосов
/ 02 июня 2018

Я бы сделал это так;похоже, что функция dmy из пакета lubridate может распознать все форматы даты в вашем примере, но если у вас более широкий набор строк дат, которые могут не всегда храниться:

# lubridate parses your dates in dmy function
df$date_try <- dmy(df$V1) 
# the ones that are not NA must be dates
ind <- c(which(!is.na(df$date_try)))
# insert some bookends at the index locations before and after your dates
new_ind <- c(seq_along(df$date_try), ind + 0.5, ind - 0.5)
new_V1 <- c(df$V1, rep("date", length(ind) * 2))

# currently the bookends are at the end of the list,
# we must re-order them to insert at the proper locations
# create your desired output dataframe
df.new <- data.frame(V1 = new_V1[order(new_ind)])

> head(df.new)
             V1
1         stuff
2          date
3 2nd June 2018
4          date
5   otherstuff1
6      baseball
0 голосов
/ 02 июня 2018

Необходимо выполнить три шага:

  • найти позиции даты (используя grep)
  • создать новый data.frame с пробелом для date строк
  • добавить date к новому data.frame

Код:

# Find position of `month year`
foo <- grep(paste(month.name, "\\d+$", collapse = "|"), df$V1)
# Expand original data.frame with space for data
dfDesired <- data.frame(x = df$V1[sort(c(1:nrow(df), foo, foo))], stringsAsFactors = FALSE)
# Find position for date in expanded data.frame
bar <- foo + seq(by = 2, length.out = length(foo))
# Add date
dfDesired$x[c(bar - 1, bar + 1)] <- "date"

Примечания:

grep выполняется со строкой:paste(month.name, "\\d+$", collapse = "|")

"Январь \ d + $ | Февраль \ d + $ | Март \ d + $ | Апрель \ d + $ | Май \ d + $ | Июнь \ d + $ | Июль \ d + $ | Август \"d + $ | сентябрь \ d + $ | октябрь \ d + $ | ноябрь \ d + $ | декабрь \ d + $ "

Нам нужно bar позиций, поскольку строки в новых data.frame перемещаются на: 1, 3,5 + * * +1028

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...