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语言常用基础知识(入门)
data.frame 动态确定列名称 var <- "mpg" #Doesn't work mtcars$var #These both work, but note tha ...
- Java学习-033-JavaWeb_002 -- 网页标记语言JSP基础知识
JSP 是 Sun 公司提倡的一门网页技术标准.在 HTML 文件中,加入 Java 代码就构成了 JSP 网页,当 Web 服务器访问 JSP 请求的时候,首先执行其中的 Java 程序源码,然后以 ...
- R语言语法基础二
R语言语法基础二 重塑数据 增加行和列 # 创建向量 city = c("Tampa","Seattle","Hartford"," ...
- R语言语法基础一
R语言语法基础一 Hello world #这里是注释 myString = "hello world" print(myString) [1] "hello world ...
- 【R】R语言常用函数
R语言常用函数 基本 一.数据管理vector:向量 numeric:数值型向量 logical:逻辑型向量character:字符型向量 list:列表 data.frame:数据框c:连接为向量或 ...
- 【值得收藏】C语言入门基础知识大全!从C语言程序结构到删库跑路!
01 C语言程序的结构认识 用一个简单的c程序例子,介绍c语言的基本构成.格式.以及良好的书写风格,使小伙伴对c语言有个初步认识. 例1:计算两个整数之和的c程序: #include main() { ...
- iOS开发系列--C语言之基础知识
概览 当前移动开发的趋势已经势不可挡,这个系列希望浅谈一下个人对IOS开发的一些见解,这个IOS系列计划从几个角度去说IOS开发: C语言 OC基础 IOS开发(iphone/ipad) Swift ...
- 3011C语言_基础知识
第一章 基础知识 1.1 基本框架 //任何一个c语言程序都必须包括以下格式: int main(int argc, char *argv[] ) { : } //这是c语言的基本结构,任何一个程 ...
- R语言编程艺术(3)R语言编程基础
本文对应<R语言编程艺术> 第7章:R语言编程结构: 第9章:面向对象的编程: 第13章:调试 ============================================== ...
随机推荐
- 跨站点端口攻击 – XSPA(SSPA)
许多Web应用程序提供的功能将数据从其他Web服务器,由于种种原因.下载XML提要,从远程服务器,Web应用程序可以使用用户指定的URL,获取图像,此功能可能会被滥用,使制作的查询使用易受攻击的Web ...
- [HTML/CSS]display:none和visibility:hidden的区别
写在前面 在群里有朋友问这样一个问题,display:none的标签,影响了布局.这就引出了本篇这样的问题,印象中display:none的块元素是不占位置的. 一个例子 <!DOCTYPE h ...
- 为 PHP 开发者准备的 12 个调试工具
PHP是在实践中发展迅速并被最多使用的脚本语言:包含了诸如详细的文档.庞大的社区.无数可使用的脚本及支持框架等许多特性.PHP提供的这些特性使得它比Python或Ruby等脚本语言更容易上手. 为构建 ...
- ObjectStore onFetch方法获取记录总数
转自:http://blog.csdn.net/earthhour/article/details/38686029 ObjectStore onFetch方法获取记录总数 require(['doj ...
- js函数延迟执行
function delay(value){ //全局变量保存当前值 window._myTempDalayValue = value; setTimeout(function(){ //延时之后与全 ...
- linux yum 命令 详解
linux yum命令详解 yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能 ...
- HDU 4493 Tutor(精度处理)
题目 #include<stdio.h> int main() { int t; double a,s; scanf("%d",&t); while(t--) ...
- HDU 1686 Oulipo , 同 POJ 3461 Oulipo (字符串匹配,KMP)
HDU题目 POJ题目 求目标串s中包含多少个模式串p KMP算法,必须好好利用next数组,, (kmp解析)——可参考 海子的博客 KMP算法 //写法一: #include<string ...
- uva 10817
Problem D: Headmaster's Headache Time limit: 2 seconds The headmaster of Spring Field School is cons ...
- swift学习网站
http://letsswift.com/category/swiftguide/http://www.imooc.com/course/list?is_easy=3&c=ioshttp:// ...