Home > Enterprise >  Dataframe consisting of mixed class, how to convert that into numerical in R
Dataframe consisting of mixed class, how to convert that into numerical in R

Time:07-20

Tried this code sapply(df,function(x) as.numeric(as.character(x))), gives an error and is removing all the observation

CodePudding user response:

Try this

df[] <- lapply(df , as.numeric)

CodePudding user response:

This is the output:

data.frame':    753 obs. of  52 variables:
 $ Language                         : num  0 0 0 0 5 0 0 0 1 0 ...
 $ Ethinicity                       : num  0 0 0 0 4 0 0 0 0 0 ...
 $ Nationality                      : num  0 0 0 0 4 0 0 0 3 0 ...
 $ MartialStatus                    : num  2 3 2 3 3 1 3 2 3 3 ...
 $ SexualPreference                 : num  NA NA NA NA 0 0 0 2 0 0 ...
 $ Covid19                          : num  NA NA NA NA 1 1 1 0 1 1 ...
 $ Mood                             : num  0 1 0 1 NA 0 1 0 NA 1 ...
 $ Little interest                  : chr  "2" "4" "1" "4" ...
 $ Feeling down                     : chr  "2" "4" "1" "4" ...
 $ Trouble falling or staying asleep: chr  "1" "4" "1" "4" ...
 $ Feeling tired                    : chr  "2" "4" "1" "4" ...
 $ Poor appetite                    : chr  "4" "4" "1" "4" ...
 $ Feeling bad about yourself       : chr  "4" "4" "4" "4" ...
 $ Troubleconcentrating             : chr  "4" "4" "1" "4" ...
 $ Moving or speaking               : chr  "4" "4" "1" "4" ...
 $ SucidialThoughts                 : chr  "4" "4" "4" "4" ...
 $ Smoking                          : num  1 0 0 0 0 NA 0 0 NA 1 ...
 $ Alcohol                          : num  1 2 1 2 0 NA 1 2 NA 1 ...
 $ Nervous                          : chr  "1" "4" "1" "4" ...
 $ ControlWorrying                  : chr  "1" "4" "1" "4" ...
 $ WorryingMore                     : chr  "4" "4" "1" "4" ...
 $ Troublerelaxing                  : chr  "4" "1" "1" "4" ...
 $ Restless                         : chr  "4" "4" "1" "4" ...
 $ Annoyed                          : chr  "4" "4" "1" "4" ...
 $ Feeling afraid                   : chr  "4" "4" "1" "4" ...
 $ PhysicalActivity                 : num  6 6 6 6 6 NA 6 6 NA 6 ...
 $ Exercises                        : chr  "1" "3" "1" "2" ...
 $ Cycling                          : chr  "1" "1" "1" "2" ...
 $ Walking                          : chr  "2" "4" "4" "4" ...
 $ Housework                        : chr  "4" "3" "4" "3" ...
 $ Gardening                        : chr  "3" "3" "3" "1" ...
 $ WalkingPace                      : num  1 2 1 1 1 NA 1 2 NA 1 ...
 $ Weight                           : num  53 67 107 83 68 44 NA 41 31 NA ...
 $ Height                           : num  155 175 176 156 170 167 NA 149 152 NA ...
 $ Stress                           : num  1 1 0 1 0 0 1 0 1 1 ...
 $ Relationship                     : chr  "2" "2" "2" "2" ...
 $ FinacialIssues                   : chr  "2" "2" "2" "2" ...
 $ Violence                         : chr  "2" "2" "2" "2" ...
 $ SeriousIllness                   : chr  "2" "2" "1" "2" ...
 $ Death                            : chr  "2" "2" "2" "2" ...
 $ Reason                           : chr  "2" "2" "2" "2" ...
 $ Support                          : chr  "2" "2" "1" "2" ...
 $ PregnacySupport                  : num  4 4 5 NA NA 1 NA 3 2 2 ...
 $ BirthPreferences                 : num  1 1 1 1 1 1 1 1 1 1 ...
 $ Maternitycare                    : num  0 0 1 0 0 1 1 0 1 2 ...
 $ BabyFeeding                      : num  0 0 2 0 0 0 0 0 1 2 ...
 $ Occupation                       : num  1 1 1 1 NA 0 NA 1 1 1 ...
 $ Education                        : num  3 2 5 2 NA 0 3 1 2 4 ...
 $ Employment                       : num  1 1 5 5 NA 2 5 5 1 1 ...
 $ EmploymentType                   : num  1 1 NA 0 0 NA 1 0 0 1 ...
 $ FamilyMembers                    : num  NA 2 1 NA 3 1 2 2 2 2 ...
 $ Income                           : num  0 0 0 0 0 0 0 0 0 0 ...
  • Related