I am a new R user. Currently I am working on a dataset wherein I have to transform the multiple binary columns into single factor column
Here is the example:
current dataset like :
$ Property.RealEstate : num 1 1 1 0 0 0 0 0 1 0 ...
$ Property.Insurance : num 0 0 0 1 0 0 1 0 0 0 ...
$ Property.CarOther : num 0 0 0 0 0 0 0 1 0 1 ...
$ Property.Unknown : num 0 0 0 0 1 1 0 0 0 0 ...
Property.RealEstate Property.Insurance Property.CarOther Property.Unknown
1 0 0 0
0 1 0 0
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Recoded column should be:
Property
1 Real estate
2 Insurance
3 Real estate
4 Insurance
5 CarOther
6 Unknown
It is basically a reverse of melt.matrix
function.
Thank You all for your Precious Inputs. It does work. But one issue though, I have some rows which takes value as:
Property.RealEstate Property.Insurance Property.CarOther Property.Unknown
0 0 0 0
I want these to be marked as NA or Null
Would be a help if you suggest on this as well.
Thank You
Create a new factor based upon the index of each 1 in each row Use the matrix column names as the labels for each level
also you can try:
also use
Tomas
solution - ifounf somewhere in SOAlternately, you can just do it in the naïve way. I think it's more transparent than any of the other suggestions (including my other suggestion).
Something different:
Get the data:
Reshape it:
If you want it cleaned up, do:
If you prefer a factor output:
...and cleaned up:
Melt is certainly a solution. I'd suggest using the reshape2 melt as follows: