Syntax question - When to use the letter "c"

I'm unclear on when to use the letter c. I've seen that it's used to mean "combine"
ex. c(1,2,3),

however, can it also mean column?
data[row, c("column name")]

I thought rows and columns can be indicated as data[x,y], without the "c" ?

It does not mean columns, it is showing the same behaviour, to create a vector of columns to be selected

data[row, c("columnname1", "columname2",....)]
data[row, c(1,3:5,7,....)]

And it works the same with rows

data[c(1,4,5:9), c(1,3)]

For selecting rows 1,4,5,6,7,8,9, and columns 1 and 3

You use 'c' to select more than 1 row or columns, by names or position (numbers) with the exception of consecutive ones, like

data[5:9, 2:4]

Where you select rows 5,6,7,8,9 and columns 2,3,4.

5 Likes

Well it's not a letter, it's a function c() that " Combine Values into a Vector or List", so you need to use the function c() whenever you need to pass a vector or list as a parameter, like in your example

You are passing a character vector with the names of your columns.

In this case, the "income" column is dropped:

allCols <- colnames(data.test)
drop <- c("Income")
keep <- setdiff(allCols, drop)
keep

In line #2 how do we know this is referring to a column? i'm used to seeing [,column] when referring to a column.

This is not referring to a column name per se, it's a character vector of length 1 containing the string "Income", from the context you know that it's also the name of one of the columns.

2 Likes

I am not sure what you want to do with your code, as it i not reproducible, but in line two it is not know to what it is referring (Well, as Andres say, you may know by context, but at that point, R has no idea of what is it, rather than just a vector of length 1 with a string on it). It is just a string It will depend on the context it is used: It can be used to select rows, columns, lists, to subset a data frame.
Think in drop = 5. What is drop? Just a number. You can use to select row number 5, column number 5, the fifth element of a list, to subset a data frame by removing all values larger than 5 etc... drop <-c('Income') has the same behaviour

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.