Multiple Separators for the same file input R

后端 未结 3 1160
一个人的身影
一个人的身影 2020-12-30 08:17

I\'ve had a look for answers, but have only found things referring to C or C#. I realise that much of R is written in C but my knowledge of it is non-existent. I am also re

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 08:36

    Try this:

    # dummy data
    df <- read.table(text="
    Name    Name1   *XYZ_Name3_KB_MobApp_M-18-25_AU_PI ANDROID  2013-09-32 14:39:55.0   2013-10-16 13:58:00.0   0   218 4   93  1377907200000
    Name    Name2   *CCC_Name3_KB_MobApp_M-18-25_AU_PI ANDROID  2013-09-32 14:39:55.0   2013-10-16 13:58:00.0   0   218 4   93  1377907200000
    ", as.is = TRUE)
    
    # replace "_" to "-"
    df_V3 <- gsub(pattern="_", replacement="-", df$V3, fixed = TRUE)
    
    # strsplit, make dataframe
    df_V3 <- do.call(rbind.data.frame, strsplit(df_V3, split = "-"))
    
    # output, merge columns
    output <- cbind(df[, c(1:2)],
                    df_V3,
                    df[, c(4:ncol(df))])
    

    Building on the comments below, here is another related option, but one which uses read.table instead of strsplit.

    splitCol <- "V3"
    temp <- read.table(text = gsub("-", "_", df[, splitCol]), sep = "_")
    names(temp) <- paste(splitCol, seq_along(temp), sep = "_")
    cbind(df[setdiff(names(df), splitCol)], temp)
    

提交回复
热议问题