r - 如何从R中的linux进程状态(ps)命令读取输出?

标签 r memory io system text-formatting

这是 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/

相关文章:

r - 为多个 shapefile 保存 writeOGR

R - 需要帮助加速 for 循环

r - 根据多个变量计算排名

sql - result_fetch(res@ptr, n)' : nanodbc/nanodbc. cpp:2966: 07009: [Microsoft][ODBC Driver 13 for SQL Server]无效的描述符索引

c - 如何管理硬件缓存使用

c++ - 在 C++ 中取消读取文件

ruby - Net::HTTPResponse 主体作为 IO

python - 为什么 int 在 Python 中需要三倍的内存?

C++检查变量以解决越界问题

linux - Linux 中的文件漏洞是如何工作的