简体   繁体   中英

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.

We can try with 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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