简体   繁体   中英

Split each string inside an R data frame

First I must state that I am absolutely noob in R. I have only reviewed the tutorial codes.

I needs to transform my current state:

timeseries <- as.data.frame(
  unlist(
    strsplit(
      rawToChar(
        output$stdout
      ),
      "\n"
    )
  )
)
                                   V1
1  DEV+M0001000 1514779200 1571637600
2  DEV+M0001CAL 1567108800 1567195200
3  DEV+M0003000 1514779200 1571637600
4  DEV+M0003CAL 1567105200 1567108800
5  DEV+M0004000 1514779200 1571637600
6  DEV+M0004CAL 1567108800 1567195200

…Into a two-dimensional data frame like:

             V1         V2         V3
1  DEV+M0001000 1514779200 1571637600
2  DEV+M0001CAL 1567108800 1567195200
3  DEV+M0003000 1514779200 1571637600
4  DEV+M0003CAL 1567105200 1567108800
5  DEV+M0004000 1514779200 1571637600
6  DEV+M0004CAL 1567108800 1567195200

Any advice? Or link to the same problem?

You can split the strings and then combine the output into a data frame using base R:

as.data.frame(do.call(rbind, strsplit(df$V1, " ")))

#### OUTPUT ####
            V1         V2         V3
1 DEV+M0001000 1514779200 1571637600
2 DEV+M0001CAL 1567108800 1567195200
3 DEV+M0003000 1514779200 1571637600
4 DEV+M0003CAL 1567105200 1567108800
5 DEV+M0004000 1514779200 1571637600
6 DEV+M0004CAL 1567108800 1567195200

You can also try tidyr 's separate :

library(tidyr)
separate(df, V1, c("V1", "V2", "V3"), " ")

#### OUTPUT ####
            V1         V2         V3
1 DEV+M0001000 1514779200 1571637600
2 DEV+M0001CAL 1567108800 1567195200
3 DEV+M0003000 1514779200 1571637600
4 DEV+M0003CAL 1567105200 1567108800
5 DEV+M0004000 1514779200 1571637600
6 DEV+M0004CAL 1567108800 1567195200

We can try using sub here for a base R option:

df$V2 <- sub("^\\S+ (\\S+) \\S+", "\\1", df$V1)
df$V3 <- sub("^\\S+ \\S+ ", "", df$V1)
df$V1 <- sub(" \\S+ \\S+$", "", df$V1)
df
            V1         V2         V3
1 DEV+M0001000 1514779200 1571637600
2 DEV+M0001CAL 1567108800 1567195200

Data:

df <- data.frame(V1=c("DEV+M0001000 1514779200 1571637600",
                      "DEV+M0001CAL 1567108800 1567195200"), stringsAsFactors=FALSE)
df
                                  V1
1 DEV+M0001000 1514779200 1571637600
2 DEV+M0001CAL 1567108800 1567195200

Data:

df <- data.frame(V1=c("DEV+M0001000 1514779200 1571637600",
                              "DEV+M0001CAL 1567108800 1567195200"), stringsAsFactors=FALSE)


df$V2 <- "whatever"

df$V3 <- "how you going"

Split the string into new columns (works regardless of the number of elements in df$V1 & how many vectors comprise df):

split_df <- cbind(df[, colnames(df) != "V1", drop = FALSE],

                  data.frame(do.call("rbind", strsplit(df$V1, "\\s+"))))

colnames(split_df) <- c(names(df[, names(df) != "V1"]),

                        as.character(c(
                          gsub(".*[$]", "", deparse(substitute(df$V1))),

                          paste0(rep("V", length(grep(
                            "\\s+", df$V1
                          ))),

                          c(ncol(df) + 1):(length(grep(
                            "\\s+", df$V1
                          )) + ncol(df)))
                        )))
names(split_df)

It can be firstly written as a matrix of 3 columns for sub-strings, and then make it to the data frame.

as.data.frame(matrix(sapply(timeseries,function(v) unlist(strsplit(v,split = " "))), ncol=3, byrow = TRUE))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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