简体   繁体   English

将制表符分隔的字符串转换为数据框

[英]Convert tab delimited string to dataframe

I have a long character string that looks like this, except where I've shown double back slashes there is, in reality, only one backslash. 我有一个长字符串,看起来像这样,除了我在上面显示了双反斜杠的地方,实际上只有一个反斜杠。

char.string <- "BAT\\tUSA\\t\\tmedium\\t0.8872\\t9\\tOff production\\tCal1|Cal2\\r\\nGNAT\\tCAN\\t\\small\\t0.3824\\t11\\tOff production\\tCal3|Cal8|Cal9\\r\\n"

I tried the following. 我尝试了以下方法。

df <- data.frame(do.call(rbind, strsplit(char.string, "\t", fixed=TRUE)))

df <- ldply (df, data.frame)

The first returns a vector. 第一个返回向量。 The second returns thousands of rows and two columns, one consisting of sequential numbers and the second consisting of all the data. 第二个返回数千行和两列,其中一个由序号组成,第二个由所有数据组成。

I'm trying to achieve this: 我正在努力实现这一目标:

item = c("BAT", "GNAT")
origin = c("USA", "CAN")
size = c("medium", "small")
lot = c("0.8872", "0.3824")
mfgr = c("9", "11")
stat = c("Off production", "Off production")
line = c("Cal1|Cal2", "Cal3|Cal8|Cal9")

df = data.frame(item, origin, size, lot, mfgr, stat, line)
df

  item origin   size    lot mfgr           stat           line
1  BAT    USA medium 0.8872    9 Off production      Cal1|Cal2
2 GNAT    CAN  small 0.3824   11 Off production Cal3|Cal8|Cal9

read.table() should actually be just fine here, but you have two basic problems: 在这里, read.table()实际上应该没问题,但是您有两个基本问题:

  1. There's two typos 有两个错别字
    a. 一种。 I'm assuming you don't want \\\\small , but rather small 我假设您不希望\\\\small ,而是small
    b. You have \\\\t\\\\tmedium where I think you want just \\\\tmedium 您有\\\\t\\\\tmedium ,我想您只想要\\\\tmedium
  2. "\\\\t" is not the same as "\\t" "\\\\t""\\t"

Try this 尝试这个

# Start with your original input
char.string <- "BAT\\tUSA\\t\\tmedium\\t0.8872\\t9\\tOff production\\tCal1|Cal2\\r\\nGNAT\\tCAN\\t\\small\\t0.3824\\t11\\tOff production\\tCal3|Cal8|Cal9\\r\\n"
# Eliminate the typos
char.string <- sub("\\\\s", "s", char.string)
char.string <- sub("\\\\t\\\\t", "\\\\t", char.string)
# Convert \\t, etc. to actual tabs and newlines
char.string <- gsub("\\\\t", "\t", char.string)
char.string <- gsub("\\\\r", "\r", char.string)
char.string <- gsub("\\\\n", "\n", char.string)
# Read the data into a dataframe
df <- read.table(text = char.string, sep = "\t")
# Add the colnames
colnames(df) <- c("item", "origin", "size", "lot", "mfgr", "stat", "line")
# And take a look at the result
df



  item origin   size    lot mfgr           stat           line
1  BAT    USA medium 0.8872    9 Off production      Cal1|Cal2
2 GNAT    CAN  small 0.3824   11 Off production Cal3|Cal8|Cal9

I took some liberties with what I think are typos in your char.string . 我对char.string中的错别字有所保留

library(tidyverse)

char.string <- "BAT\\tUSA\\tmedium\\t0.8872\\t9\\tOff production\\tCal1|Cal2\\r\\nGNAT\\tCAN\\tsmall\\t0.3824\\t11\\tOff production\\tCal3|Cal8|Cal9\\n"

lapply(
    str_split(gsub("\\\\n", "", char.string), "\\\\r")[[1]]
    , function(x) {
        y <- str_split(x, "\\\\t")[[1]]
        data.frame(
            item = y[1]
            , origin = y[2]
            , size = y[3]
            , lot = y[4]
            , mfgr = y[5]
            , stat = y[6]
            , line = y[7]
            , stringsAsFactors = F
        )
    }) %>%
    bind_rows()

  item origin   size    lot mfgr           stat           line
1  BAT    USA medium 0.8872    9 Off production      Cal1|Cal2
2 GNAT    CAN  small 0.3824   11 Off production Cal3|Cal8|Cal9

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

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