简体   繁体   中英

Character vector to dataframe in R

I have the following character vector:

da <- c("    1 1 0 0 0 0 0 7.071", "    2 0 0 1 0 1 1 3.033", "    3 1 1 0 0 1 0 4.095")

I would like it to become a dataframe where each split value is a new cell and each element in the character is a new row. So it would become:

1 | 1 | 0 | 0 | 0 | 0 | 0 | 7.071
2 | 0 | 0 | 1 | 0 | 1 | 1 | 3.033
3 | 1 | 1 | 0 | 0 | 1 | 0 | 4.095

Coeercing straight to a dataframe or a list then a dataframe does not work.

  • One base R option is to use read.table + paste
df <- read.table(text = paste(da,sep = "\n"),
                 header = FALSE)
  • Or try strsplit + rbind , eg,
df <- as.data.frame(do.call(rbind,strsplit(trimws(da)," ")))

such that

> df
  V1 V2 V3 V4 V5 V6 V7    V8
1  1  1  0  0  0  0  0 7.071
2  2  0  0  1  0  1  1 3.033
3  3  1  1  0  0  1  0 4.095

First you want to trim the leading whitespace using stringr::str_trim , and then split each substring up by the space delim using stringr::str_split . This returns a nested list that you can unlist, convert to a matrix and then data.frame.

da <- c("    1 1 0 0 0 0 0 7.071", "    2 0 0 1 0 1 1 3.033", "    3 1 1 0 0 1 0 4.095")
a = stringr::str_split(stringr::str_trim(da), pattern=" ")
df = data.frame(matrix(unlist(a), nrow=length(a), byrow=T))

Not terribly elegant, but it works.

library(dplyr)
library(stringr)

da <- da %>%
  str_trim() %>%
  str_split(., " ") %>%
  data.frame %>%
  t

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