Convert data to 0 1 matrix in R
1
0
Entering edit mode
2.2 years ago

Hi

I have a 2 column data file .txt and I wanna convert it into a 0 1 matrix using R

Sample input

Column1 x1 x2 x3…..x100 Column2: male female both….unknown

Desired output

Column1: x1 x2 x3…..x100 Row1: male female both unknown In x1 row and male column print 1 all others 0 Same way in x2 row and female column print 1 others 0

rstudio R • 473 views
ADD COMMENT
1
Entering edit mode
2.2 years ago
Ram 43k

Use tidyr::pivot_longer (or reshape2::melt) and then dplyr::mutate with an ifelse. You should be able to write the exact code using these hints.

ADD COMMENT

Login before adding your answer.

Traffic: 2372 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6