如何将字符向量拆分为数据帧? [英] How to split a character vector into data frame?

查看:71
本文介绍了如何将字符向量拆分为数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R还是比较陌生,希望您能再次帮助我。我有一个长度为42000的字符向量。向量看起来像这样:

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("blablabla-19960101T000000Z-1.tsv", "blablabla-19960101T000000Z-2.tsv", "blablabla-19960101T000000Z-3.tsv")

我想将向量分割成如下所示的数据帧:

I want to split the vector into a data frame which looks like this:

Name        Date          no
blablabla   1996-01-01    1
blablabla   1996-01-01    2
blablabla   1996-01-01    3

我正在努力拆分以及创建数据框。有人可以帮我弄这个吗?谢谢!

I'm struggling with the splitting as well as the creation of my data frame. Can someone help me with this? Thanks!

推荐答案

DF <- data.frame(do.call(rbind, strsplit(a, "-", fixed=TRUE)))
DF[,2] <- as.Date(DF[,2] , format="%Y%m%d")
DF[,3] <- as.integer(gsub(".tsv", "", DF[,3], fixed=TRUE))

#         X1         X2 X3
#1 blablabla 1996-01-01  1
#2 blablabla 1996-01-01  2
#3 blablabla 1996-01-01  3

这篇关于如何将字符向量拆分为数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆