Darae-Uri
Darae-Uri

Reputation: 103

How to delete a row and a column

Libraries I use:

library(ggplot2)
library(dplyr)
library(statsr)



```{r}
factor_sc <- table(gss1$class, gss1$getahead) 
factor_sc <- addmargins(factor_sc)
factor_sc 
```

I write this and the output is:

               Hard Work Both Equally Luck Or Help Other   Sum
  Lower Class        1063          368          299     0  1730
  Working Class     10229         3221         1870     0 15320
  Middle Class       9914         3624         1612     0 15150
  Upper Class         701          265          100     0  1066
  No Class              0            0            0     0     0
  Sum               21907         7478         3881     0 33266

I want to run the chi-square inference on this data so that I want to remove Others and No class.

However, I already remove them using:

```{r} 
gss1 <- gss %>%   filter(!is.na(getahead),
!is.na(class), class != "No Class", getahead !="Other") 
```

Why do Other and No class appear in my table?

Upvotes: 0

Views: 44

Answers (2)

Dan Chaltiel
Dan Chaltiel

Reputation: 8494

There is a little misunderstanding here. filter is used to remove lines of your dataset. What you want here is to change your class to NA when it is "No Class". mutate is what you need.

Try this code:

gss1 <- gss %>% 
  mutate(class = ifelse(class=="No Class", NA, class), 
         getahead = ifelse(getahead =="Other", NA, getahead )) %>% 
  select(class, getahead) %>% 
  table %>% 
  chisq.test

It is possible that you need to use NA_character_ instead of NA.

You could just remove these lines and columns from factor_sc by writing this:

factor_sc[-5,-4] %>% chisq.test

Upvotes: 1

Amith DS
Amith DS

Reputation: 1

Try simple df$column <- NULL for column and similar for row. If the attempt is to achieve something bigger good luck.

Upvotes: 0

Related Questions