简体   繁体   English

将R中的变量名称拆分为几列

[英]split a column into several column with variable names in R

How can I separate the one column into several columns. 如何将一列分为几列。 Note that the I have the information on the width of each column. 请注意,我具有每列宽度的信息。

eg 例如

12729404
28290282
36383025

I would like to separate it and give it a variable names into: 我想将其分开并给它一个变量名:

plot_no    sp_code   dbh
127         29           40.4
282         90           28.2
363         83           10.2

Note that I have ~5000 rows data with one column. 请注意,我有〜5000行数据和一列。

We can try with sub 我们可以尝试sub

read.table(text=sub("(\\d{3})(\\d{2})(\\d{2})(\\d{1})", "\\1,\\2,\\3.\\4", df1$v1),
             sep=",", header=FALSE, col.names = c('plot_no', 'sp_code', 'dbh'))
#  plot_no sp_code  dbh
#1     127      29 40.4
#2     282      90 28.2
#3     363      83  2.5

data 数据

df1 <- structure(list(v1 = c(12729404L, 28290282L, 36383025L)), 
.Names = "v1", class = "data.frame", row.names = c(NA, -3L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM