简体   繁体   English

将一列拆分为不同的列

[英]Split a column into different columns

I read a .txt file using read.table() : 我使用read.table()读取了一个.txt文件:

head(read.table("file1.txt",header=FALSE),4)
         V1
 1 1334523578
 2 4332535535
 3 7899854289
 4 1435353587

How can I split this column efficiently to three different columns? 如何将该列有效地分为三个不同的列? First three digits fall into column1, next four digits into column2, and the last column would have three digits. 前三位数字属于列1,后四位数字属于列2,最后一列将具有三位数字。 I tried: 我试过了:

  as.data.frame(matrix(as.numeric(sapply(data.frame(rbind(c(1,4,8),c(3,7,10))), function(x) substr(d1$V1, x[1], x[2]))),4,3))

   V1   V2  V3
1 133 4523 578
2 433 2535 535
3 789 9854 289
4 143 5353 587

But, this is not looking good. 但是,这看起来并不好。

Supposing your dataframe is called df , you can do that also with substr : 假设您的数据帧称为df ,则也可以使用substr

df2 <- data.frame(V1 = substr(df$V1, 1, 3), 
                  V2 = substr(df$V1, 4, 7), 
                  V3 = substr(df$V1, 8, 10))

Another option would be to use the separate -function from the tidyr -package: 另一种选择是使用与tidyr separatetidyr

library(tidyr)
df2 <- separate(df, V1, c('V1','V2','V3'), sep = c(3,7))

Both options give: 这两个选项都提供:

> df2
   V1   V2  V3
1 133 4523 578
2 433 2535 535
3 789 9854 289
4 143 5353 587

Used data: 使用的数据:

df <- structure(list(V1 = c(1334523578, 4332535535, 7899854289, 1435353587)), 
                .Names = "V1", class = "data.frame", row.names = c(NA, -4L))

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

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