咕噜声选项:
library(purrr)
'total=7871MB;free=5711MB;used=2159MB;shared=0MB;buffers=304MB;cached=1059MB;
free=71MB;total=5751MB;shared=3159MB;used=5MB;buffers=30MB;cached=1059MB;
cached=1059MB;total=5751MB;shared=3159MB;used=5MB;buffers=30MB;free=109MB;' %>%
strsplit('\n') %>% .[[1]] %>% # separate lines into character vector
strsplit(';') %>% # separate each line into a list of key-value pairs
map(strsplit, '=') %>% # split key-value pairs into length-2 sublists
map(transpose) %>% # flip list of key-value pairs to list of keys and values
map_dfr(~set_names(.x[[2]], .x[[1]])) # set names of values to keys and simplify to data frame
#> # A tibble: 3 x 6
#> total free used shared buffers cached
#> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 7871MB 5711MB 2159MB 0MB 304MB 1059MB
#> 2 5751MB 71MB 5MB 3159MB 30MB 1059MB
#> 3 5751MB 109MB 5MB 3159MB 30MB 1059MB
或更以数据框为中心的选项:
library(tidyverse)
# put text in data frame
data_frame(text = 'total=7871MB;free=5711MB;used=2159MB;shared=0MB;buffers=304MB;cached=1059MB;
free=71MB;total=5751MB;shared=3159MB;used=5MB;buffers=30MB;cached=1059MB;
cached=1059MB;total=5751MB;shared=3159MB;used=5MB;buffers=30MB;free=109MB;') %>%
separate_rows(text, sep = '\n') %>% # separate lines into separate rows
rowid_to_column('line') %>% # add index for each line to help spreading later
separate_rows(text, sep = ';') %>% # separate each line into key-value pairs
filter(text != '') %>% # drop extra entries from superfluous semicolons
separate(text, c('key', 'value')) %>% # separate keys and values into columns
spread(key, value) %>% # reshape to wide form
select(-line) # drop line index column
#> # A tibble: 3 x 6
#> buffers cached free shared total used
#> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 304MB 1059MB 5711MB 0MB 7871MB 2159MB
#> 2 30MB 1059MB 71MB 3159MB 5751MB 5MB
#> 3 30MB 1059MB 109MB 3159MB 5751MB 5MB
如果你想避免包,你可以通过read.dcf 破解它,它读取 Debian 控制格式(如 R 包说明文件),它只是键值对。 DCF 使用: 而不是= 和\n 而不是;,所以你需要先做一点gsubing:
junk <- 'total=7871MB;free=5711MB;used=2159MB;shared=0MB;buffers=304MB;cached=1059MB;
free=71MB;total=5751MB;shared=3159MB;used=5MB;buffers=30MB;cached=1059MB;
cached=1059MB;total=5751MB;shared=3159MB;used=5MB;buffers=30MB;free=109MB;'
junk <- gsub('=', ':', junk)
junk <- gsub(';', '\n', junk)
mat <- read.dcf(textConnection(junk))
mat
#> total free used shared buffers cached
#> [1,] "7871MB" "5711MB" "2159MB" "0MB" "304MB" "1059MB"
#> [2,] "5751MB" "71MB" "5MB" "3159MB" "30MB" "1059MB"
#> [3,] "5751MB" "109MB" "5MB" "3159MB" "30MB" "1059MB"
它返回一个矩阵,但它的格式很好,很容易转换为适当的数据帧:
df <- as.data.frame(mat, stringsAsFactors = FALSE)
df
#> total free used shared buffers cached
#> 1 7871MB 5711MB 2159MB 0MB 304MB 1059MB
#> 2 5751MB 71MB 5MB 3159MB 30MB 1059MB
#> 3 5751MB 109MB 5MB 3159MB 30MB 1059MB