R言語常用基礎知識

5265 ワード


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 columndata$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 columndata$size      <- NULLdata[["size"]] <- NULLdata[,"size"]  <- NULLdata[[3]]      <- NULLdata[,3]       <- NULLdata           <- 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"