nginx proxy minio 預設頁配置(三)
Introductionto R
Factor
- Factor 可以把向量Vector轉換成因子
- 可以沒有order,比如動物種類,我們不能說猴子比大象高階;也可以有order,比如氣溫高低,顯然是有順序的
factor_temperature_vector <- factor(temperature_vector, order = TRUE, levels = c("Low", "Medium", "High"))
- Factor Levels
levels(factor_survey_vector) <- c("Female", "Male")
順序很重要,原來的Level順序是"F","M",一定要對應
- 用Summary來觀察結果
# Generate summary for survey_vector 這裡是向量 summary(survey_vector) Length Class Mode 5 character character # Generate summary for factor_survey_vector 這裡是因子 summary(factor_survey_vector) Female Male 2 3
- Ordered factors
# Create factor_speed_vector speed_vector <- c("medium", "slow", "slow", "medium", "fast") factor_speed_vector <- factor(speed_vector, ordered = TRUE, levels = c("slow", "medium", "fast"))#在這裡設定的level是從小到大的順序 # Factor value for second data analyst da2 <- factor_speed_vector[2] # Factor value for fifth data analyst da5 <-factor_speed_vector[5] # Is data analyst 2 faster than data analyst 5? FALSE=No da2>da5 [1] FALSE
可以比較大小
Have a look at the structure
The function
str()
shows you the structure of your data set. Applying thestr()
function will often be the first thing that you do when receiving a new data set or data frame. It is a great way to get more insight in your data set before diving into the real analysis.For a data frame it tells you:
- The total number of observations (e.g. 32 car types)
- The total number of variables (e.g. 11 car features)
- A full list of the variables names (e.g.
mpg
,cyl
… )- The data type of each variable (e.g.
num
)- The first observations
# Investigate the structure of mtcars str(mtcars) 'data.frame': 32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... $ cyl : num 6 6 4 6 8 6 8 4 4 6 ... $ disp: num 160 160 108 258 360 ... $ hp : num 110 110 93 110 175 105 245 62 95 123 ... $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ... $ wt : num 2.62 2.88 2.32 3.21 3.44 ... $ qsec: num 16.5 17 18.6 19.4 17 ... $ vs : num 0 0 1 1 0 1 0 1 1 1 ... $ am : num 1 1 1 0 0 0 0 0 0 0 ... $ gear: num 4 4 4 3 3 3 3 4 4 4 ... $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
Daraframe
選擇Dataframe中元素的方法
用[1,2]選擇第一行第二列,或者[1,"列的名字"]
# Select first 5 values of diameter column planets_df[1:5,'diameter'] [1] 0.382 0.949 1.000 0.532 11.209
用$直接選擇一列
# Select the rings variable from planets_df rings_vector <- planets_df$rings # Print out rings_vector rings_vector [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
# Select planets with diameter < 1 subset(planets_df,subset = diameter<1) name type diameter rotation rings 1 Mercury Terrestrial planet 0.382 58.64 FALSE 2 Venus Terrestrial planet 0.949 -243.02 FALSE 4 Mars Terrestrial planet 0.532 1.03 FALSE >
List
# Adapt list() call to give the components names my_list <- list(my_vector, my_matrix, my_df) names(my_list) = c('vec','mat','df')
How to construct and name lists.
# Finish the code to build shining_list
shining_list <- list(moviename = mov, actors = act, reviews = rev)
Selecting elements from a list
Your list will often be built out of numerous elements and components. Therefore, getting a single element, multiple elements, or a component out of it is not always straightforward.
One way to select a component is using the numbered position of that component. For example, to "grab" the first component ofshining_list
you type
shining_list[[1]]
A quick way to check this out is typing it in the console. Important to remember: to select elements from vectors, you use single square brackets:[ ]
. Don't mix them up!
You can also refer to the names of the components, with[[ ]]
or with the$
sign. Both will select the data frame representing the reviews:
shining_list[["reviews"]]
shining_list$reviews
Besides selecting components, you often need to select specific elements out of these components. For example, withshining_list[[2]][1]
you select from the second component,actors
(shining_list[[2]]
), the first element ([1]
). When you type this in the console, you will see the answer is Jack Nicholson.