2

How to convert dataframe into sparse matrix? For example, please see below the dataframe. Need to see this dataframe as a whole factor with levels 1~10. Convert levels into 10 columns. Convert 0 to 1 based on what factor it has it each Row in V1~V4. Which will become something like the Expected Outcome.

Take Row 1 in Expected Outcome for example, column No1, No2, No5, No8 == 1 because V1~V4 has the number 1,8,2,5.

Update: Have tried solution Create Sparse Matrix from a data frame , cannot get it to match my Expected Outcome

Dataframe

    V1   V2   V3   V4  
1   1    8    2    5
2   6    7    9    3
3   6    2    3    2
4   5    8    9    10
5   4    3    5    1
6   3    9    1    10

Expected Outcome

    V1   V2   V3   V4   No1 No2 No3 No4 No5 No6 No7 No8 No9 No10 
1   1    8    2    5     1   1   0   0   1   0   0   1   0   0
2   6    7    9    3     0   0   1   0   0   1   1   0   1   0
3   6    2    3    4     0   1   1   1   0   1   0   0   0   0  
4   5    8    9    10    0   0   0   0   1   0   0   1   1   1
5   4    3    5    1     1   0   1   1   1   0   0   0   0   0
6   3    9    1    10    1   0   1   0   0   0   0   0   1   1
Intern Ne
  • 65
  • 7

0 Answers0