Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

how to replace the coulomn of a factor variable with its dummy variable in R?

$
0
0

i have a variable name type in my data. it include all type of values like this: type='Hp','apple','unknown','nokia',... now i want to create a dummy variable that only show me which one is "Hp' and put one for it and 0 for all other types. i used this code:

MyData$type<- factor ( with ( MyData, ifelse ( ( type== 'Hp' ), 1 , 0 ) ) )

this should work true i think but it returns 0 for even 'Hp's, so i try to check the following code

dummy<- factor ( with ( MyData, ifelse ( ( type== 'Hp' ), 1 , 0 ) ) )

and dummy was correct ! so i want to know what is the problem?why its incorrect when i want to put it directly in my data frame?

if it helps

structure(list(type= structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 5L, 5L, 5L), .Label = c("Hp", "nokia", 
"apple 1", "apple 2", "unknown"), class = "factor"), 
    Year = c(2002L, 2002L, 2002L, 2002L, 2002L, 
    2002L, 2002L, 1974L, 1974L, 1974L)), row.names = c(NA, 10L
), class = "data.frame")

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>