【问题标题】:Sing split in data frame by semicolon delimiter?用分号分隔符分割数据帧?
【发布时间】:2018-09-30 10:24:47
【问题描述】:

我想在不使用第三方包的情况下使用 R 在数据框中拆分以分号分隔的文本。

我有以下数据。

> #To view the first 6 rows of the data
> head(bank1)
  age.job.marital.education.default.housing.loan.contact.month.day_of_week.duration.campaign.pdays.previous.poutcome.emp.var.rate.cons.price.idx.cons.conf.idx.euribor3m.nr.employed.y
1                                                                      56;housemaid;married;basic.4y;no;no;no;telephone;may;mon;261;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no
2                                                               57;services;married;high.school;unknown;no;no;telephone;may;mon;149;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no
3                                                                   37;services;married;high.school;no;yes;no;telephone;may;mon;226;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no
4                                                                         40;admin.;married;basic.6y;no;no;no;telephone;may;mon;151;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no
5                                                                   56;services;married;high.school;no;no;yes;telephone;may;mon;307;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no
6                                                                  45;services;married;basic.9y;unknown;no;no;telephone;may;mon;198;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no

请帮助我根据标题中的列名将数据分成不同的列。

提前致谢。

【问题讨论】:

  • 导入数据时,指定;为字段分隔符。这是一个选择吗?
  • 欢迎来到 SO!您如何在 r 中导入数据? read.csv?其他?如果您无法处理输入端口,您能否发布dput(head(bank1)) 的结果而不是head(bank1)?这将使您更容易在 R 中复制和粘贴数据。
  • @markus 我试过这样做。但是,标题由 . 分隔。而数据由 ; 分隔
  • @s_t 但是,我有近 45000 行。我很难复制粘贴每个数据,对吧?
  • @AradhyaMudigonda,你是对的,这就是为什么我建议你将 head() 嵌套在 dput() 函数中。

标签: r dataframe split delimiter


【解决方案1】:

您可以将data.frame 列连接到character 字符串并再次运行read.table。但是请注意,列名的数量(28)不等于列的数量(21)。此外,列标题和观察的分隔符不同(看起来像标题的空格,观察的分号)。

请看下面的代码:

df <- structure(list(age.job.marital.education.default.housing.loan.contact.month.day_of_week.duration.campaign.pdays.previous.poutcome.emp.var.rate.cons.price.idx.cons.conf.idx.euribor3m.nr.employed.y = structure(c(4L, 
6L, 1L, 2L, 5L, 3L), .Label = c("37;services;married;high.school;no;yes;no;telephone;may;mon;226;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no", 
"40;admin.;married;basic.6y;no;no;no;telephone;may;mon;151;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no", 
"45;services;married;basic.9y;unknown;no;no;telephone;may;mon;198;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no", 
"56;housemaid;married;basic.4y;no;no;no;telephone;may;mon;261;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no", 
"56;services;married;high.school;no;no;yes;telephone;may;mon;307;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no", 
"57;services;married;high.school;unknown;no;no;telephone;may;mon;149;1;999;0;nonexistent;1.1;93.994;-36.4;4.857;5191;no"
), class = "factor")), row.names = c(NA, -6L), class = "data.frame")

z <- paste(df[, 1], sep = "", collapse = "\n")
df2 <- read.table(text = z, header = FALSE, sep = ";")

nms2 <- unlist(strsplit(names(df), "\\."))[-(22:28)]
names(df2) <- nms2
str(df2)

输出:

'data.frame':   6 obs. of  21 variables:
 $ age        : int  56 57 37 40 56 45
 $ job        : Factor w/ 3 levels "admin.","housemaid",..: 2 3 3 1 3 3
 $ marital    : Factor w/ 1 level "married": 1 1 1 1 1 1
 $ education  : Factor w/ 4 levels "basic.4y","basic.6y",..: 1 4 4 2 4 3
 $ default    : Factor w/ 2 levels "no","unknown": 1 2 1 1 1 2
 $ housing    : Factor w/ 2 levels "no","yes": 1 1 2 1 1 1
 $ loan       : Factor w/ 2 levels "no","yes": 1 1 1 1 2 1
 $ contact    : Factor w/ 1 level "telephone": 1 1 1 1 1 1
 $ month      : Factor w/ 1 level "may": 1 1 1 1 1 1
 $ day_of_week: Factor w/ 1 level "mon": 1 1 1 1 1 1
 $ duration   : int  261 149 226 151 307 198
 $ campaign   : int  1 1 1 1 1 1
 $ pdays      : int  999 999 999 999 999 999
 $ previous   : int  0 0 0 0 0 0
 $ poutcome   : Factor w/ 1 level "nonexistent": 1 1 1 1 1 1
 $ emp        : num  1.1 1.1 1.1 1.1 1.1 1.1
 $ var        : num  94 94 94 94 94 ...
 $ rate       : num  -36.4 -36.4 -36.4 -36.4 -36.4 -36.4
 $ cons       : num  4.86 4.86 4.86 4.86 4.86 ...
 $ price      : int  5191 5191 5191 5191 5191 5191
 $ idx        : Factor w/ 1 level "no": 1 1 1 1 1 1

【讨论】:

  • 非常感谢@Artem
  • @AradhyaMudigonda 如果解决了您的问题,请考虑accept this answer
猜你喜欢
  • 2020-10-01
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多