Question: I was trying to order a data frame in R according to a column named 'credit card usage'. The name of the data frame is mydata. The following command without a comma gives an error 

> 
> newdata = mydata[order('credit card usage')]
> 
> 
> 

But the following command with a comma works absolutely fine 

> 
> newdata = mydata[order('credit card usage'),] 
> 
> 
> 

I need to understand why do we need the comma. Please can someone explain in a simple language whats going on behind the scenes?

Also the following command 

> 
> mydata[order('credit card usage'),] 
> 
> 
> 

gives only the first row and not the whole dataframe. Why?

Answer: 
Why `mydata[order('credit card usage'),]` returns only `first row` is a tricky one.

The name of the column when used after `,` in `[` refers to value of column otherwise it is just a `string`. 

`order('credit card usage')` call considers just a `string` is passed to it and it sorts it out and pass the index(which is `1`). Hence:
`mydata[order('credit card usage'),]` reduces to

`mydata[1,]`

=> which is `1st row` of the `mydata`.
MKR's answer explains why the OP obtained the results described in the post. Here, we'll explain how to deal with the column named `credit card usage` to correctly sort the entire data frame. 

Generally speaking, it's not advisable to use column names containing spaces in R because it often leads to unexpected results as experienced by the OP. 

To use a column in a data frame whose name contains spaces, one must use the `[[` form of the extract operator. We'll illustrate with some sample data...

```
set.seed(95014123)
mydata <- data.frame(matrix(round(runif(100)*1000,0),nrow=50))
names(mydata) <- c("credit card usage","value")
head(mydata)
head(mydata[order(mydata[["credit card usage"]]),])

```

...and the output:

```
> set.seed(95014123)
> mydata <- data.frame(matrix(round(runif(100)*1000,0),nrow=50))
> names(mydata) <- c("credit card usage","value")
> head(mydata)
  credit card usage value
1               795   217
2               816   613
3               342   323
4               126   751
5               618   780
6               625   529
> head(mydata[order(mydata[["credit card usage"]]),])
   credit card usage value
47                25   109
44                81   534
18                91   985
31                99   931
19               109   190
4                126   751
> 

```

One can replace the spaces with underscores via the `gsub()` function, which will enable one to use the `$` form of the extract operator in subsequent functions. 

```
# replace spaces with underscores
names(mydata) <- gsub(" ","_",names(mydata))
head(mydata[order(mydata$credit_card_usage),])

```

...and the output:

```
> # replace spaces with underscores
> names(mydata) <- gsub(" ","_",names(mydata))
> head(mydata[order(mydata$credit_card_usage),])
   credit_card_usage value
47                25   109
44                81   534
18                91   985
31                99   931
19               109   190
4                126   751
> 

```