『Data Science』R语言学习笔记,基础语法
发布时间:2021-02-27 06:18:56 所属栏目:大数据 来源:网络整理
导读:副标题#e# Data Types Data Object Vector x - c(0.5,0.6) ## numericx - c(TRUE,FALSE) ## logicalx - c(T,F) ## logicalx - c("a","b","c") ## characterx - 9:29 ## integerx - c(1+0i,2+4i) ## complexx - vector("numeric",length = 10) ## create a n
|
Multiple objects can be deparsed using the dump function and read back in using source. > x <- "foo" ## create the first data object
> y <- data.frame(a = 1,b = "a") ## create the second data object
> dump(c("x","y"),file = "data.R") ## store the both data object in to a file called 'data.R'
> rm(x,y) ## remove the both data object from RAM
> source("data.R") ## import the dumped file 'data.R'
> y ## print the data object 'y' from 'data.R'
a b
1 1 a
> x ## print the data object 'x' from 'data.R'
[1] "foo"
Connections: Interfaces to the Outside WorldData are read in using connection interfaces. Connections can be made to files (most common) or to other more exotic things.
> con <- file('db.txt','r')
> readLines(con)
Subsetting
Basic> x <- c("a","c","d","e")
> x[1]
[1] "a"
> x[2]
[1] "b"
> x[1:3]
[1] "a" "b" "c"
> x[x > "a"]
[1] "b" "c" "d" "e"
> u <- x>"a"
> u
[1] FALSE TRUE TRUE TRUE TRUE
> x[u]
[1] "b" "c" "d" "e"
Lists> x <- list(foo = 1:4,bar = 0.6) > x[1] $foo [1] 1 2 3 4 > x[[1]] [1] 1 2 3 4 > x[[2]] [1] 0.6 > x$bar [1] 0.6 > x$foo [1] 1 2 3 4 > x[["bar"]] [1] 0.6 > x["bar"] $bar [1] 0.6 > x <- list(foo = 1:4,bar = 0.6,baz = "hello") > x[c(1,3)] $foo [1] 1 2 3 4 $baz [1] "hello" > name <- "foo" > x[[name]] [1] 1 2 3 4 > x$name ## `name` is a variable,not a `level`,so does not has x$name in the list `x`. NULL > x$foo [1] 1 2 3 4 Matrices(编辑:PHP编程网 - 金华站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐


