如何将这样的变量转换为具有多列的数据框?
1 18.0 8 307.0 130.0 3504. 12.0 70 1 chevrolet chevelle malibu
2 15.0 8 350.0 165.0 3693. 11.5 70 1 buick skylark 320
3 18.0 8 318.0 150.0 3436. 11.0 70 1 plymouth satellite
4 16.0 8 304.0 150.0 3433. 12.0 70 1 amc rebel sst
5 17.0 8 302.0 140.0 3449. 10.5 70 1 ford torino
6 15.0 8 429.0 198.0 4341. 10.0 70 1 ford galaxie 500
导入的数据使其成为单列而不是多列。我尝试了分离,但将数字分隔到小数点。
auto.mpg %>% separate(V1,c("mpg","cylinders","displacement","power","weight","acceleration","year","origin"))
mpg cylinders displacement power weight acceleration year origin V2
1 18 0 8 307 0 130 0 3504 chevrolet chevelle malibu
2 15 0 8 350 0 165 0 3693 buick skylark 320
3 18 0 8 318 0 150 0 3436 plymouth satellite
4 16 0 8 304 0 150 0 3433 amc rebel sst
5 17 0 8 302 0 140 0 3449 ford torino
6 15 0 8 429 0 198 0 4341 ford galaxie 500
当前数据集:
答案 0 :(得分:1)
您可以尝试以下方法:
auto.mpg <- as.data.frame(do.call(rbind, (strsplit(as.character(auto.mpg$V1), " {2,10}"))))
auto.mpg
# V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
# 1 1 18.0 8 307.0 130.0 3504. 12.0 70 1 chevrolet chevelle malibu
# 2 2 15.0 8 350.0 165.0 3693. 11.5 70 1 buick skylark 320
# 3 3 18.0 8 318.0 150.0 3436. 11.0 70 1 plymouth satellite
# 4 4 16.0 8 304.0 150.0 3433. 12.0 70 1 amc rebel sst
# 5 5 17.0 8 302.0 140.0 3449. 10.5 70 1 ford torino
# 6 6 15.0 8 429.0 198.0 4341. 10.0 70 1 ford galaxie 500
然后
auto.mpg <- auto.mpg[,-1]
names(auto.mpg) <- c("mpg", "cylinders", "displacement", "power",
"weight", "acceleration", "year", "origin", "model")
auto.mpg
# mpg cylinders displacement power weight acceleration year origin model
# 1 18.0 8 307.0 130.0 3504. 12.0 70 1 chevrolet chevelle malibu
# 2 15.0 8 350.0 165.0 3693. 11.5 70 1 buick skylark 320
# 3 18.0 8 318.0 150.0 3436. 11.0 70 1 plymouth satellite
# 4 16.0 8 304.0 150.0 3433. 12.0 70 1 amc rebel sst
# 5 17.0 8 302.0 140.0 3449. 10.5 70 1 ford torino
# 6 15.0 8 429.0 198.0 4341. 10.0 70 1 ford galaxie 500