0

I am trying to subset a data frame by using the cbind() function and $ selecting the colum names:

stormData <- read.csv("/Users/b.w.h/Documents/R/Coursera/Reproducible Research /Project 2/repdata-data-StormData.csv");
stormDataSubset <- as.data.frame(c(stormData$STATE, stormData$EVTYPE, stormData$FATALITIES, stormData$INJURIES));
head(stormDataSubset);

But it only returns the index column and the state column:

Why isn't this subsetting method working? And what should I do to properly subset it? Thanks!

paul-shuvo
  • 1,874
  • 4
  • 33
  • 37
Kaihua Hou
  • 89
  • 1
  • 9
  • Please visit [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – UseR10085 Jul 18 '20 at 06:26
  • Use **dplyr** and then use the **select()** function. – kashj Jul 18 '20 at 08:18
  • 1
    @kashj why not just `data[c("var1", "var2", "var3")]`? If this issue is just to select variables, it's no need to use an external package. – Darren Tsai Jul 18 '20 at 09:09
  • @DarrenTsai, you're right actually. I am wondering how it just didn't cross my mind. Anyways, I hope Kaihua Hou won't mind learning something extra. – kashj Jul 18 '20 at 11:01

3 Answers3

1

In your case the columns are added one below the another, making it only one column. You can use

stormDataSubset <- cbind.data.frame(stormData$STATE, stormData$EVTYPE, stormData$FATALITIES, stormData$INJURIES)
UseR10085
  • 7,120
  • 3
  • 24
  • 54
0

Use dplyr

stormDataSubset <- stormData %>% select(STATE,EVTYPE,FATALITIES,INJURIES)

If all these columns are continuous and in sequence, then it's better to use

stormDataSubset <- stormData %>% select(STATE:INJURIES)

And there you go. It's done.

kashj
  • 136
  • 8
-1

Try using cbind

stormDataSubset <- as.data.frame(cbind(stormData$STATE, stormData$EVTYPE, stormData$FATALITIES, stormData$INJURIES))

Dhiraj
  • 1,650
  • 1
  • 18
  • 44
  • 1
    It's dangerous to `cbind()` multiple vectors and then `as.data.frame()`. If these vectors have multiple classes, e.g. `numeric` and `character`, `cbind()` will change them all to characters. So after `as.data.frame()`, you will get a data with overall character columns. – Darren Tsai Jul 18 '20 at 09:03
  • 1
    A better alternative is `cbind.data.frame()`, which does not change data types. – Darren Tsai Jul 18 '20 at 09:06