How to split a character vector into data frame?

前端 未结 4 2117
南旧
南旧 2020-12-15 12:47

I\'m still relatively new to R and hope you can again help me. I have a character vector with a length of 42000. The vector looks like this:

a <- c(\"blab         


        
4条回答
  •  死守一世寂寞
    2020-12-15 13:11

    I know I'm late to this party, but I wanted to see this same idea in a magrittr pipe and using more tidyverse functions. Here's what I've got:

    library(stringr)
    library(lubridate)
    library(tidyverse)
    
    a <- c("blablabla-19960101T000000Z-1.tsv", "blablabla-19960101T000000Z-2.tsv", "blablabla-19960101T000000Z-3.tsv")
    
    a %>%
    strsplit('-') %>%
    transpose() %>%
    map_dfc(~data_frame(.x)) %>%
    unnest() %>%
    set_names(c('Name','Date','no')) %>% 
    mutate(Date = Date %>%
            str_extract('\\d+') %>% 
            ymd(),
            no = str_extract(no, '\\d+'))
    

提交回复
热议问题