这是 data.txt:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 2280 728 ? Ss 20:44 0:00 init [2]
root 2 0.0 0.0 0 0 ? S 20:44 0:00 [kthreadd]
root 202 0.0 0.0 0 0 ? S< 20:44 0:00 [ext4-dio-unwri
root 334 0.0 0.1 2916 1452 ? Ss 20:44 0:00 udevd --daemon
如何将数据读入data.frame?
1.不能决定分隔符
最后一个字段有问题,
space
不能是分隔符,init [2] ,udevd --daemon 是一个字段,不能用
space
分隔.2.无固定宽度
每条线都有不同的宽度。
那么,如何将 data.txt 读入 data.frame?
最佳答案
我会这样做:
library(stringr) # has a convenient function for splitting to a fixed length
raw <- system("ps aux", intern = TRUE)
fields <- strsplit(raw[1], " +")[[1]]
ps <- str_split_fixed(raw[-1], " +", n = length(fields))
colnames(ps) <- fields
关于r - 如何从R中的linux进程状态(ps)命令读取输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14432052/