Specifying datatypes for columns
When you read data from a text or CSV file, you should specify the names and data types for each column. The read() function will try to determine if the first entry of the dataset contains the column names. R is clever at figuring out some datatypes, but if you are reading a categorical variable coded as 0, 1, and 2, it will read it as a numeric variable, and you will need to specify the data type for that column after reading the data.
本练习是课程的一部分
Multivariate Probability Distributions in R
练习说明
- Assign the new column names to the
winedataset, then check that they have been correctly assigned. - Change the
Typecolumn into a factor with three levels. - Use the
str()function to check the data type/structure before and after changing the data type.
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Assign new names
___ <- c('Type', 'Alcohol', 'Malic', 'Ash', 'Alcalinity', 'Magnesium', 'Phenols', 'Flavanoids', 'Nonflavanoids','Proanthocyanins', 'Color', 'Hue', 'Dilution', 'Proline')
# Check the new column names
___
# Check data type/structure of each variable
str(___)
# Change the Type variable data type
___
# Check data type/structure again
___