如何替换数据框中的值? (逻辑回归)

时间:2014-09-30 19:38:28

标签: r

structure(list(age = c(33L, 21L, 37L, 29L, 45L), workclass = structure(c(4L, 
7L, 4L, 4L, 4L), .Label = c(" Federal-gov", " Local-gov", " Never-worked", 
" Private", " Self-emp-inc", " Self-emp-not-inc", " State-gov", 
" Without-pay"), class = "factor"), fnlwgt = c(319854L, 41183L, 
103323L, 176027L, 264526L), education = structure(c(10L, 16L, 
12L, 10L, 8L), .Label = c(" 10th", " 11th", " 12th", " 1st-4th", 
" 5th-6th", " 7th-8th", " 9th", " Assoc-acdm", " Assoc-voc", 
" Bachelors", " Doctorate", " HS-grad", " Masters", " Preschool", 
" Prof-school", " Some-college"), class = "factor"), education.num = c(13L, 
10L, 9L, 13L, 12L), marital.status = structure(c(6L, 5L, 5L, 
5L, 1L), .Label = c(" Divorced", " Married-AF-spouse", " Married-civ-spouse", 
" Married-spouse-absent", " Never-married", " Separated", " Widowed"
), class = "factor"), occupation = structure(c(10L, 10L, 3L, 
10L, 7L), .Label = c(" Adm-clerical", " Armed-Forces", " Craft-repair", 
" Exec-managerial", " Farming-fishing", " Handlers-cleaners", 
" Machine-op-inspct", " Other-service", " Priv-house-serv", " Prof-specialty", 
" Protective-serv", " Sales", " Tech-support", " Transport-moving"
), class = "factor"), relationship = structure(c(2L, 4L, 2L, 
2L, 4L), .Label = c(" Husband", " Not-in-family", " Other-relative", 
" Own-child", " Unmarried", " Wife"), class = "factor"), race = structure(c(5L, 
5L, 5L, 5L, 5L), .Label = c(" Amer-Indian-Eskimo", " Asian-Pac-Islander", 
" Black", " Other", " White"), class = "factor"), sex = structure(c(2L, 
1L, 2L, 1L, 2L), .Label = c(" Female", " Male"), class = "factor"), 
capital.gain = c(4650L, 0L, 0L, 0L, 0L), capital.loss = c(0L, 
0L, 0L, 0L, 0L), hours.per.week = c(35L, 20L, 40L, 40L, 40L
), native.country = structure(c(39L, 39L, 39L, 39L, 39L), .Label = c(" Cambodia", 
" Canada", " China", " Columbia", " Cuba", " Dominican-Republic", 
" Ecuador", " El-Salvador", " England", " France", " Germany", 
" Greece", " Guatemala", " Haiti", " Holand-Netherlands", 
" Honduras", " Hong", " Hungary", " India", " Iran", " Ireland", 
" Italy", " Jamaica", " Japan", " Laos", " Mexico", " Nicaragua", 
" Outlying-US(Guam-USVI-etc)", " Peru", " Philippines", " Poland", 
" Portugal", " Puerto-Rico", " Scotland", " South", " Taiwan", 
" Thailand", " Trinadad&Tobago", " United-States", " Vietnam", 
" Yugoslavia"), class = "factor"), RESULT = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = c(" <=50K", " >50K"), class = "factor")), .Names = c("age", 
"workclass", "fnlwgt", "education", "education.num", "marital.status", 
"occupation", "relationship", "race", "sex", "capital.gain", 
"capital.loss", "hours.per.week", "native.country", "RESULT"), row.names = c(25231L, 
17952L, 24945L, 25524L, 11025L), class = "data.frame")

我有这个数据框。在最后一列有一个名为&#34; RESULT。&#34;的变量。 它只有两种值。 &#34; &LT; = 50K&#34;&#34; &GT; 50K&#34;

我尝试使用这些数据进行逻辑回归,但它没有用。我认为原因是因为变量RESULT不被视为二进制。

所以我想修改数据集。 &#34; &LT; = 50K&#34;进入0和&#34; &GT; 50K&#34;进入1。 我怎么能这样做?

4 个答案:

答案 0 :(得分:3)

或者

c(0, 1)[as.numeric(df$RESULT)]
## [1] 0 0 0 0 0

仅适用于这项运动,因为@Richards评论,一些基准

创建相对较大的数据集

n <- 1e6
set.seed(123)
RESULT <- factor(sample(c("<=50K", ">50K"), n, replace = TRUE))

定义不同的方法

DMT <- function(x) ifelse(x == "<=50K", 0, 1)
David <- function(x) c(0, 1)[as.numeric(x)]
Richard <- function(x) grepl("[>]", x) + 0

验证所有答案给出相同的结果

all.equal(DMT(RESULT), David(RESULT), Richard(RESULT))
# [1] TRUE

运行一些基准测试(当然我赢了)

library(microbenchmark)
microbenchmark(DMT(RESULT),
               David(RESULT),
               Richard(RESULT))

# Unit: milliseconds
#            expr       min        lq      mean   median       uq       max neval
#     DMT(RESULT) 373.58323 379.22780 402.52273 386.8819 423.1997 512.12878   100 # Looser
#   David(RESULT)  10.50121  10.73885  13.03632  11.5627  12.3372  56.57713   100 # Winner
# Richard(RESULT) 104.98288 106.73164 113.26356 108.5151 112.9743 159.13860   100 # Nice try Richard :)

答案 1 :(得分:2)

df$RESULT<-ifelse(df$RESULT=="<=50K", 0, 1)

答案 2 :(得分:2)

您可以将逻辑从grepl转换为数字。在这种情况下,所有值都变为零

grepl("[>]", df$RESULT)+0
# [1] 0 0 0 0 0

grepl的一个更好的例子是

grepl("[>]", factor(c(as.character(df$RESULT), ">50k")))+0
# [1] 0 0 0 0 0 1

答案 3 :(得分:0)

如果您的data.frame已保存到变量zz,那么您可以这样做:

zz$RESULT <- factor(zz$RESULT,levels=c(" <=50K"," >50K"),labels=c(0,1))