【发布时间】:2017-03-24 15:16:14
【问题描述】:
您好,我对 R 相当陌生,并且正在尝试自学,我想将以下代码集转换为 for 循环/函数,但我在努力解决如何实现这一点,因为没有一组统一的可能条目。
这里是非函数形式的代码:
colnames(x)[36] <- "DriveToFoodSource_BIN"
x[,36][x[,36] == "Drive"] <- "1"
x[,36][x[,36] == ""] <- "0"
colnames(x)[37] <- "BusToFoodSource_BIN"
x[,37][x[,37] == "Ride the bus"] <- "1"
x[,37][x[,37] == ""] <- "0"
colnames(x)[38] <- "TaxiToFoodSource_BIN"
x[,38][x[,38] == "Taxi cab"] <- "1"
x[,38][x[,38] == ""] <- "0"
colnames(x)[39] <- "FriendsBringFoodSource_BIN"
x[,39][x[,39] == "Friend"] <- "1"
x[,39][x[,39] == ""] <- "0"
colnames(x)[40] <- "WalkToFoodSource_BIN"
x[,40][x[,40] == "Walk"] <- "1"
x[,40][x[,40] == ""] <- "0"
colnames(x)[41] <- "DeliveryFoodSource_BIN"
x[,41][x[,41] == "Food is delivered"] <- "1"
x[,41][x[,41] == ""] <- "0"
colnames(x)[42] <- "OtherWayToFoodSource_BIN"
x[,42][x[,42] == "Drive"] <- "1"
x[,42][x[,42] == ""] <- "0"
【问题讨论】: