Home > Net >  Trying to order character values in a column based on name
Trying to order character values in a column based on name

Time:12-15

y<-data.frame(householdincome$State,householdincome$Income.Level,householdincome$Percent.of.Total)
factor(y$householdincome.Income.Level,levels = c("$15,000 to $19,999","$20,000 to $24,999","$200,000 or more"))
y[order(y$householdincome.Income.Level),]

I need to order the output starting with 200,000 or more, however my data frame output disregards all values of that name and only lists certain values in the column that i am not looking for. Any help would be greatly appreciated. Thanks!

CodePudding user response:

Does this solve your problem?

householdincome.State = c("AZ", "TX", "MT", "NY", "PA")
householdincome.Level = c("$15,000 to $19,999","$20,000 to $24,999","$200,000 or more", "$15,000 to $19,999","$20,000 to $24,999")
householdincome.Percent.of.Total = c(runif(5, 0, 100))

y <- data.frame(householdincome.State,
                householdincome.Level,
                householdincome.Percent.of.Total)

factor(y$householdincome.Level, levels = c("$15,000 to $19,999","$20,000 to $24,999","$200,000 or more"))
#> [1] $15,000 to $19,999 $20,000 to $24,999 $200,000 or more   $15,000 to $19,999
#> [5] $20,000 to $24,999
#> Levels: $15,000 to $19,999 $20,000 to $24,999 $200,000 or more
y[order(y$householdincome.Level, decreasing = TRUE),]
#>   householdincome.State householdincome.Level householdincome.Percent.of.Total
#> 3                    MT      $200,000 or more                         95.45595
#> 2                    TX    $20,000 to $24,999                         59.78140
#> 5                    PA    $20,000 to $24,999                         71.03190
#> 1                    AZ    $15,000 to $19,999                         48.62256
#> 4                    NY    $15,000 to $19,999                         62.28109

Created on 2021-12-15 by the reprex package (v2.0.1)

CodePudding user response:

Alternatively you could use [order(y$Level,decreasing=TRUE),]

householdincome<-data.frame("State" = c("London", "Reading", "Southampton"), 
                 "Level" = c("$15,000 to $19,999","$20,000 to $24,999","$200,000 or more"),
                 "Percent.of.Total" = c(runif(3, 0, 100)))


y<-data.frame(householdincome)
y$Level<-factor(y$Level)

y[order(y$Level,decreasing=TRUE),]

        State              Level Percent.of.Total
3 Southampton   $200,000 or more         10.37236
2     Reading $20,000 to $24,999         66.84539
1      London $15,000 to $19,999         98.65112
  • Related