R Language - variable data type

Language data classification R

More data types R language, but they are dynamic statement that the variable is not declared as a data type. R target variable is assigned

  • vector
  • List
  • matrix
  • Array
  • Data Frame
  • Factor
    following are some of the most simple object type
# Atomic vector of type character.
print("abc");#character

# Atomic vector of type double.
print(12.5)#numeric

# Atomic vector of type integer.
print(63L)#integer

# Atomic vector of type logical.
print(TRUE)#logical

# Atomic vector of type complex.
print(2+3i)#complex

# Atomic vector of type raw.
print(charToRaw('hello'))#raw

Vector vector

The simplest is the vector type, namely the use c()of a declaration.
The following example, if one of the elements is a character, then the value is cast non-character type character

# The logical and numeric values are converted to characters.
s <- c('apple','red',5,TRUE)
print(s)

In fact, the multi-element vector can be represented by a colon, such as

v <- 6.6:12.6
print(v)
w <- 3.8:11.4

That represents from 6.6 to 12.6, incremented by a vector of ones; W represents from 3.8 incremented by 1 to 10.8. You can also use function to create:

# Create vector with elements from 5 to 9 incrementing by 0.4.
print(seq(5, 9, by = 0.4))

If one of the elements is a character, then the value is cast non-character type character.

# The logical and numeric values are converted to characters.
s <- c('apple','red',5,TRUE)
print(s)

Access vector elements, may be used '[]' is populated as condition index. And, support vector value calculation, but it must be the same size.

List list

Create a list with the list of functions, and which can contain virtually any type of data, to be named list each element.

# Create a list containing a vector, a matrix and a list.
list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3))

# Give names to the elements in the list.
names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

# Show the list.
print(list_data)

List Access

Access list elements either by a direct index number, you can also use the name index

# Access the first element of the list.
print(list_data[1])

# Access the list element using the name of the element.
print(list_data$A_Matrix)

List of operations

List element is manipulated, direct assignment. Also you can merged.list <- c(list1,list2)merge list.

# Convert the lists to vectors.
v1 <- unlist(list1)
v2 <- unlist(list2)

Matrix matrix

The basic syntax to create a matrix in the R language is

matrix(data, nrow, ncol, byrow, dimnames)
  • Data input vector in a matrix of data elements.
  • nrow is the number of rows to be created.
  • ncol is the number of columns to be created.
  • byrow is a logical clue. If TRUE, the input vector elements are arranged in rows.
  • dimname is the name assigned to the rows and columns.

    Access matrix elements

    Direct access to the matrix elements the subscript access matrix filled in square brackets, i.e. \ (A_ 23 is {} = M [2,3] \) . Or labeled with a single click direct access to an entire row or column, i.e. \ (A_ 13 is {}, {23 is A_}, \ cdots, A_ M3} = {M [,. 3] \) .

    Matrix calculation

    Use R operators perform various mathematical operations on the matrix. The result of the operation is a matrix. For the matrix operation involved, the dimensions (number of rows and columns) should be the same.

    Array

    R is an array of data objects stored in two or more dimensions of data. The following example creates the array is actually performed, and the steps array named:
# Create two vectors of different lengths.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
column.names <- c("COL1","COL2","COL3")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")

# Take these vectors as input to the array.
result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names, matrix.names))
print(result)

Similarly, access to the array is similar to the matrix, the array has more than three dimensions, can be extracted with a bracket, and two commas access, or a plurality of elements

print(array[1,3,4])
print(array[3, ,2])
print(array[2, , ])

Elements of the array operation is performed by a part of the element array accesses. Such as using a comma and a digital two dimensions, the matrix is extracted.
We can use the apply()function calculated on the elements in the array.

apply(x, margin, fun)
  • x is an array.
  • margin is the name of the data set used.
  • fun to be applied is a function of the array elements
    and thus calculates the internal array

    factor

    R in the language, a variable nominal and ordinal variables can be expressed with a factor. Syntax is
f <- factor(x=charactor(), levels, labels=levels, exclude = NA, ordered = is.ordered(x), namax = NA)
  • levels: level factor data, the default is not repeated in x values;
  • labels: Identifies the name of a certain level, the level-one correspondence, to facilitate identification, the default value is taken levels;
  • exclude: remove x from the level values, default values ​​NA;
  • ordered: logic value, whether there are factor level order (coding order), if taken TRUE, otherwise take FALSE;
  • nmax: limiting the number of levels.
    GL () function is used to define a regular factor vector syntax is as follows
gl(n, k, length = n*k, labels = 1:n, ordered = FALSE)
  • n: a positive integer representing the number of levels of Factor
  • k: positive integer representing the number of repetitions of each level;
  • length: positive integer representing the vector length factor, default n * k
  • labels: indicates the name factor levels, the default value is 1: n
  • ordered: logical variable, indicating whether there is a factor in order, the default value FALSE
    and factor()function can be converted into vector data factor. Briefly, factor is finite sequence having a period of two yuan hierarchical order, Print print out is its hierarchical level. Each column of the data frame (data.frame) can also be seen in the factor.
v <- gl(3, 4, labels = c("Tampa", "Seattle","Boston"))
print(v)
# 结果为
Tampa   Tampa   Tampa   Tampa   Seattle Seattle Seattle Seattle Boston 
[10] Boston  Boston  Boston 
Levels: Tampa Seattle Boston

Data Frame

Creating a data frame

# Create data frame
new.address <- data.frame(
   city = c("Lowry", "Charlotte"),
   state = c("CO", "FL"),
   zipcode = c("80230", "33949"),
   stringsAsFactors = FALSE
)

And by str()you can see the structure of the function of the data frame. Applications can summary()obtain statistical summary nature of the data and functions. It can also be extracted

# Extract Specific columns.
result <- data.frame(emp.data$emp_name,emp.data$salary)
print(result)

# 先提取前两行,再提取所有列
# Extract first two rows.
result <- emp.data[1:2,]

# 也可以一并提取
result <- emp.data[c(3,5),c(2,4)]

To expand the data frame just add a column vector, pay attention to the data frame using the $ name is indexed using the new column name. Alternatively, the line is added with rbind()a function, with the added column cbind().

reference

https://www.w3cschool.cn/

Guess you like

Origin www.cnblogs.com/Dear-Mozart/p/11258829.html