未加星标

10 R functions for Linux commands and vice-versa

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

(This article was first published on R Open Source Automation , and kindly contributed toR-bloggers)


10 R functions for Linux commands and vice-versa

This post will go through 10 different linux commands and their R alternatives. If you’re interested in learning more R functions for working with files like some of those below, also check out this post .

How to list all the files in a directory Linux R What does it do? ls list.files() Lists all the files in a directory ls -R list.files(recursive = TRUE) Recursively lists all the files in a directory and all sub-directories ls | grep “something” list.files(pattern = “something”) Lists all the files in a directory containing the regex “something” R list.files("/path/to/directory") list.files("/path/to/do/directory", recursive = TRUE) # search for files containing "something" in their name list.files("/path/to/do/directory", pattern = "something") # search for all CSV files list.files("/path/to/do/directory", pattern = ".csv") Linux ls /path/to/directory ls -R /path/to/directory # search for files containing "something" in their name ls /path/to/directory | grep "something" # search for all CSV files ls /path/to/directory | grep ".csv" Getting the top records in a file / object Linux R What does it do? head head() Prints the top n records of a file (Linux) / data frame or other object (R) R # let df be a data frame head(df) head(df, 10) Linux head -6 some_file.txt head -10 some_file.txt Getting the current directory Linux R What does it do? pwd getwd() Gets the current directory R getwd() Linux pwd Changing the directory Linux R What does it do? cd setwd() Change the current working directory R setwd("/path/to/new/directory") Linux cd /path/to/new/directory How to count the number of files in a directory Linux R What does it do? ls -1 | wc -l length(list.files(…)) Counts the number of files in a directory R length(list.files("/path/to/some/directory")) Linux ls -1 | wc -l How to check file permissions Linux R What does it do? ls -l file.info() Returns the file permissions (Linux) / additional info (R) R file.info("/path/to/directory/file.txt")

file.inforeturns additional information about a file besides file permissions, including size, created time, last modified time, and last access time. If you just want to get permissions of the file, just run this:

file.info("/path/to/directory/file.txt")$mode

The permissions are returned in octal; to translate what this octal result means into read / write etc. abilities, see this link .

Linux ls -l /path/to/directory/file.txt How to create a new directory Linux R What does it do? mkdir dir.create() Creates a new directory R # create folder in current directory dir.create("new_folder") # create folder in different directory dir.create("/path/to/new_directory") Linux # create folder in current directory mkdir new_folder # create folder in different directory mkdir /path/to/new_directory How to create a new file Linux R What does it do? touch file.create() Creates a new file R # create a file in current directory file.create("new_file.txt") # create file in different directory file.create("/path/to/directory/new_file.txt") Linux # create a file in current directory touch new_file.txt # create file in different directory touch /path/to/directory/new_file.txt How to count the number of lines, words, and characters in a file

Though it’s possible to get the number of lines, words, and characters in a file using base R, it’s simpler to do so with the hyperSpec package.

Just use install.packages to install if needed:

install.packages("hyperSpec")

Running the below line of code will print out a data frame with the number of characters, words, and lines in the input file. Similarly, the Linux wc command will print out the same information for a file.

Linux R What does it do? wc wc() Lists the number of characters, words, and lines in a file R library(hyperSpec) wc("/path/to/directory/file.txt") Linux wc /path/to/directory/file.txt How to copy a file Linux R What does it do? cp file.copy() Copy a file R # copy file.txt to new_directory file.copy("/path/to/directory/file.txt", "/path/to/new_directory") Linux # option 1 cp /path/to/directory/file.txt /path/to/new_directory # option 2 cp /path/to/directory/file.txt /path/to/new_directory/file.txt

That’s it for this post. Happy coding! Please check out my other R posts here.

The post 10 R functions for Linux commands and vice-versa appeared first on Open Source Automation .

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

代码区博客精选文章
分页:12
转载请注明
本文标题:10 R functions for Linux commands and vice-versa
本站链接:https://www.codesec.net/view/621273.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(50)