例如:
string<-("15050505:20220513,19090909:20220515,19080808:20220513,20010101:20220515,23020202:20220515,23020402:20220515")
Run Code Online (Sandbox Code Playgroud)
首先通过拆分来识别不同的行,。这将创建一个包含一个具有 6 个元素的向量的列表,其中每个向量元素都是所需输出中的一行。
s2 <- strsplit(string, ",")
[[1]]
[1] "15050505:20220513"
[2] "19090909:20220515"
[3] "19080808:20220513"
[4] "20010101:20220515"
[5] "23020202:20220515"
[6] "23020402:20220515"
Run Code Online (Sandbox Code Playgroud)
然后再次拆分每个:,定义列和rbind()结果。这将返回一个矩阵。将其包装在as.data.frame()返回(你猜对了)一个数据帧中。
as.data.frame(do.call(rbind, strsplit(s2[[1]], ":")))
V1 V2
1 15050505 20220513
2 19090909 20220515
3 19080808 20220513
4 20010101 20220515
5 23020202 20220515
6 23020402 20220515
Run Code Online (Sandbox Code Playgroud)
还
read.table(text=gsub(',', '\n', string), sep=":", col.names = c('ID', 'date'))
ID date
1 15050505 20220513
2 19090909 20220515
3 19080808 20220513
4 20010101 20220515
5 23020202 20220515
6 23020402 20220515
Run Code Online (Sandbox Code Playgroud)
另一种紧凑的方式:
read.csv(text = c("ID,Date", chartr(":,", ",\n", string)))
ID Date
1 15050505 20220513
2 19090909 20220515
3 19080808 20220513
4 20010101 20220515
5 23020202 20220515
6 23020402 20220515
Run Code Online (Sandbox Code Playgroud)
另一种tidyverse方法:
library(tidyverse)
as_tibble(string) %>%
separate_rows(value, sep=",") %>%
separate(value, c("Id", "Date"))
Run Code Online (Sandbox Code Playgroud)
Id Date
<chr> <chr>
1 15050505 20220513
2 19090909 20220515
3 19080808 20220513
4 20010101 20220515
5 23020202 20220515
6 23020402 20220515
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
415 次 |
| 最近记录: |