R语言常用基础知识(入门)
sessionInfo() 查看R中载入的包和版本
writeLines(capture.output(sessionInfo()), "sessionInfo.txt")
sink("sessionInfo.txt")
sessionInfo()
sink()
data.frame 动态确定列名称
var <- "mpg"
#Doesn't work
mtcars$var
#These both work, but note that what they return is different
# the first is a vector, the second is a data.frame
mtcars[[var]]
mtcars[var]
data.frame 列命名
Use the colnames() function:
R> X <- data.frame(bad=1:3, worse=rnorm(3))
R> X
bad worse
1 1 -2.440467
2 2 1.320113
3 3 -0.306639
R> colnames(X) <- c("good", "better")
R> X
good better
1 1 -2.440467
2 2 1.320113
3 3 -0.306639
You can also subset:
R> colnames(X)[2] <- "superduper"
seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),
length.out = NULL, along.with = NULL, ...)
举例----------Examples----------
seq(0, 1, length.out=11)
seq(stats::rnorm(20)) #
seq(1, 9, by = 2) #
seq(1, 9, by = pi) #
seq(1, 6, by = 3)
seq(1.575, 5.125, by=0.05)
seq(17) # same as 1:17, or even better seq_len(17)
Loops
The most commonly used loop structures in R are for, while and apply loops. Less common are repeat loops. The break function is used to break out of loops, and next halts the processing of the current iteration and advances the looping index.
for(variable in sequence) {
statements
}
while(condition) statementsapply(X, MARGIN, FUN, ARGs)保存为Tab分隔的txt文件:
write.table(y, file = paste("Day",a, " ",k, ".txt", sep=""), sep = "\t",row.names=FALSE)
data.frame 添加一行:
First, we create a one-row data frame with the new data:
> newRow <- data.frame(city="West Dundee", county="Kane", state="IL", pop=5428)
Next, we use the rbind function to append that one-row data frame to our existing data frame:
> suburbs <- rbind(suburbs, newRow)
data.frame 添加一列:
my.dataframe$new.col <- a.vector
my.dataframe[, "new.col"] <- a.vector
my.dataframe["new.col"] <- a.vector
df <- data.frame(b = runif(6), c = rnorm(6))
cbind(a = 0, df)
data <- read.table(header=TRUE, text='
id weight
1 20
2 27
3 24
')
# Ways to add a column
data$size <- c("small", "large", "medium")
data[["size"]] <- c("small", "large", "medium")
data[,"size"] <- c("small", "large", "medium")
data$size <- 0 # Use the same value (0) for all rows
# Ways to remove the column
data$size <- NULL
data[["size"]] <- NULL
data[,"size"] <- NULL
data[[3]] <- NULL
data[,3] <- NULL
data <- subset(data, select=-size)
flush.console()
options(stringsAsFactors=FALSE)
A simple function to remove leading and trailing whitespace:
trim <- function( x ) {
gsub("(^[[:space:]]+|[[:space:]]+$)", "", x)
}
Usage:
> text = " foo bar baz 3 "
> trim(text)
[1] "foo bar baz 3"
在R中合并某一数据框的两列数据
需要解决的问题,需要将某一个数据框的两列值合并为一列。
在R中合并某一数据框的两列数据
浏览示例数据
> mtcars
安装tidyr包,之后加载tidyr包
> library(tidyr)
执行命令
> tidyr::unite(mtcars, "vs_am", vs, am)
将 vs 和 am 两列数据合并后,原数据列被删除了(如果想保留原数据列则通过 remove = FALSE 参数控制),新增了 vs_am 列,得到的结果如下。
在R中合并某一数据框的两列数据
个性化合并
如果在合并时想自定义连接符,可以通过参数 sep 控制,运行
> unite(mtcars, "vs_am", vs, am, sep = "ZSF", remove = FALSE)
得到的结果如下,新增 vs_am 列,连接符为 ZSF,原数据列 vs 和 am 得以保存。
R语言常用基础知识(入门)的更多相关文章
- R语言常用基础知识
seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)), length.out = NULL, along.with = NULL, ...
- Linux基础知识入门
[Linux基础]Linux基础知识入门及常见命令. 前言:最近刚安装了Linux系统, 所以学了一些最基本的操作, 在这里把自己总结的笔记记录在这里. 1,V8:192.168.40.10V1: ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro]
[易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro] 实用知识 宏Macro 我们今天来讲讲Rust中强大的宏Macro. Rust的宏macro是实现元编程的强大工具. ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理]
[易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理] 实用知识 错误处理 我们今天来讲讲Rust中的错误处理. 很多语言都有自己的错误处理方式,比如,java是异常处理机制. ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(11)|Structs结构体]
[易学易懂系列|rustlang语言|零基础|快速入门|(11)] 有意思的基础知识 Structs 我们今天来看看数据结构:structs. 简单来说,structs,就是用来封装相关数据的一种数据 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(7)|函数Functions与闭包Closure]
[易学易懂系列|rustlang语言|零基础|快速入门|(7)函数Functions与闭包Closure] 有意思的基础知识 函数Functions与闭包Closure 我们今天再来看看函数. 在Ru ...
- Hibernate入门1. Hibernate基础知识入门
Hibernate入门1. Hibernate基础知识入门 20131127 前言: 之前学习过Spring框架的知识,但是不要以为自己就可以说掌握了Spring框架了.这样一个庞大的Spring架构 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)]
[易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)] 项目实战 实战3:Http服务器 我们今天来进一步开发我们的Http服务器,用多线程实现. 我 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针]
[易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针] 实用知识 智能指针 我们今天来讲讲Rust中的智能指针. 什么是指针? 在Rust,指针(普通指针),就是保存内存地址的值 ...
随机推荐
- Atitit 架构的原则attilax总结
Atitit 架构的原则attilax总结 1.1. Rule of three称为"三次原则",指的是当某个功能第三次出现时,才进行"抽象化".是DRY原则和 ...
- [svc]jdk+tomcat部署.jforum论坛部署
安装jdk和tomcat jdk1.7.0_13(系列)下载url 我这里用的最新的jdk. 去官网下载即可 cd /usr/local/src/ tar xf jdk-8u162-linux-x64 ...
- android studio - 右键没有“New C++ Class 选项” 或 “Cannot find declaration to go to”的问题
今天在android studio的cpp文件夹上右键,发现竟然没有了New C++ Class“的选项,如下图所示: 上图是正常情况. 下图是非正常情况. 解决方案 检查”File“-"S ...
- C++中的友元函数和友元类
C++中的友元函数主要应用于以下场景: 友元函数 第一种场景 代码中有一个全局函数,该函数想要去访问某个类的成员变量(该类的成员变量是private的,且该类并未提供任何获取获取私有成员变量的publ ...
- 破解Linux系统root用户密码
linux系统的启动过程 在介绍破解Linux系统root密码之前先了解一下linux系统的启动过程 开机自检(POST),初始化部分硬件 搜素可用于引导的启动设备(如磁盘的MBR) 读取并将控制权 ...
- 每日英语:Researchers Study How Excess Fat Cells Interfere With Organ Function, Metabolism
Why are some obese people healthy, apparently protected from the damaging effects of excess fat on t ...
- cc(self)是什么意思?
分别找到这2句代码: 复制代码 cc(self):addComponent("components.behavior.EventProtocol"):exportMethods ...
- android category
本章节翻译自<Beginning-Android-4-Application-Development>,如有翻译不当的地方,敬请指出. 原书购买地址http://www.amazon.co ...
- 【Windows】windows核心编程整理(下)
windows核心编程整理(上) windows核心编程整理(下) 线程的堆栈 每当创建一个线程时,系统就会为线程的堆栈(每个线程有他自己的堆栈)保留一个堆栈空间区域,并将一些物理存储器提交给这个以保 ...
- ARKit从入门到精通(3)-ARKit自定义实现
转载:http://blog.csdn.net/u013263917/article/details/73038566 在上一小节中ARKit从入门到精通(2)-ARKit工作原理及流程介绍,我们完整 ...