• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

R语言常用基础知识(入门)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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) statements
apply(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 得以保存。



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【学习】 R语言与机器学习学习笔记(1)K-近邻算法发布时间:2022-07-18
下一篇:
用R语言在机器学习中建立集成模型?发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap