创建两个指标(虚拟)变量

时间:2018-06-10 17:34:06

标签: r statistics data-science

创建一个新的数据帧(称之为C1),它是Carseats的副本。

创建两个指标(虚拟)变量:

如果ShelveLoc =“Bad”,则Bad_Shelf = 1,否则为

如果ShelveLoc =“Good”,Good_Shelf = 1,否则为

R中的代码:

#Load the dataset carseats(in the ISLR Package) into R.Studio

library(ISLR) #load and attach the package
View(Carseats) #view the data
data("Carseats") #loads the data "Carseats"
str(Carseats) #compactly displays the internal structure of the object

#(A) Make a new dataframe (Call it C1) that is a copy of carseats

C1 <- data.frame(Carseats)

#(B) Create two indicator (dummy variables) 
#(B.1) Bad_Shelf = 1 if ShelveLoc = "Bad", 0 otherwise

Bad_Shelf <- ifelse(ShelveLoc = "Bad", 1, 0)

Good_Shelf <- ifelse(ShelveLoc = "Good", 1, 0)

**Error in ifelse(ShelveLoc = Bad, 1, 0) : 
  unused argument (ShelveLoc = Bad)

0 个答案:

没有答案