R 使用单元格中的字符串创建新列
我有一个数据框,看起来像R 使用单元格中的字符串创建新列,r,string,substr,R,String,Substr,我有一个数据框,看起来像 inx time 1 201566.202331.203500.203924.1628390 915.22 2 201571.202696.203095.203932.1628371 864.86 3 202329.203081.203090.203468.203994 743.54 4 201572.202339.203114.203507.1627763 597
inx time
1 201566.202331.203500.203924.1628390 915.22
2 201571.202696.203095.203932.1628371 864.86
3 202329.203081.203090.203468.203994 743.54
4 201572.202339.203114.203507.1627763 597.34
5 101107.201587.202689.203087.203469 592.97
6 201152.201954.202711.203506.1626167 555.01
7 200768.201586.201980.202695.1627783 542.16
8 201143.202681.202694.203935.1628369 504.30
9 202357.202697.1626161.1627741.1628368 499.81
10 201937.202324.203497.204060.1628378 499.60
而不是用句点分隔的5组数字,我想要5列+时间末尾的列我们可以使用
read.table
和sep=“.”“
将其读为5列
cbind(read.table(text = df1$inx, header = FALSE, sep="."), df1['time'])
数据
df1Adata.table
带有tstrsplit的选项
> setDT(df)[, c(Map(as.numeric, tstrsplit(inx, "\\.")), time = .(time))]
V1 V2 V3 V4 V5 time
1: 201566 202331 203500 203924 1628390 915.22
2: 201571 202696 203095 203932 1628371 864.86
3: 202329 203081 203090 203468 203994 743.54
4: 201572 202339 203114 203507 1627763 597.34
5: 101107 201587 202689 203087 203469 592.97
6: 201152 201954 202711 203506 1626167 555.01
7: 200768 201586 201980 202695 1627783 542.16
8: 201143 202681 202694 203935 1628369 504.30
9: 202357 202697 1626161 1627741 1628368 499.81
10: 201937 202324 203497 204060 1628378 499.60
> setDT(df)[, c(Map(as.numeric, tstrsplit(inx, "\\.")), time = .(time))]
V1 V2 V3 V4 V5 time
1: 201566 202331 203500 203924 1628390 915.22
2: 201571 202696 203095 203932 1628371 864.86
3: 202329 203081 203090 203468 203994 743.54
4: 201572 202339 203114 203507 1627763 597.34
5: 101107 201587 202689 203087 203469 592.97
6: 201152 201954 202711 203506 1626167 555.01
7: 200768 201586 201980 202695 1627783 542.16
8: 201143 202681 202694 203935 1628369 504.30
9: 202357 202697 1626161 1627741 1628368 499.81
10: 201937 202324 203497 204060 1628378 499.60