WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

R Β· Basic Β· question 6 of 100

What is the purpose of the c() function in R?

πŸ“• Buy this interview preparation book: 100 R questions & answers β€” PDF + EPUB for $5

In R, the c() function is used to concatenate or combine values into a single vector or list. The c() function stands for "concatenate", and is a basic and frequently used function in R.

The c() function can take any number of arguments, which can be of different data types, and returns a single object of the same data type. For example:

    # Concatenating numeric values
    x <- c(1, 2, 3, 4, 5)
    
    # Concatenating character values
    y <- c("a", "b", "c")
    
    # Concatenating values of different types
    z <- c(1, "a", TRUE)

In the first example, c(1, 2, 3, 4, 5) creates a numeric vector with the values 1 through 5. In the second example, c("a", "b", "c") creates a character vector with the values "a", "b", and "c". In the third example, c(1, "a", TRUE) creates a vector with three elements of different data types: a numeric value of 1, a character value of "a", and a logical value of TRUE. In this case, R will coerce the numeric and logical values to character values in order to create a single character vector.

The c() function can also be used to combine vectors or lists into a larger vector or list. For example:

    # Combining two numeric vectors
    a <- c(1, 2, 3)
    b <- c(4, 5, 6)
    c <- c(a, b)
    
    # Combining two lists
    list1 <- list("a", 1)
    list2 <- list("b", 2)
    list3 <- c(list1, list2)

In the first example, c(a, b) combines the a and b vectors into a single vector with the values 1 through 6. In the second example, c(list1, list2) combines the list1 and list2 lists into a single list with four elements.

In summary, the c() function in R is used to concatenate or combine values into a single vector or list. It can take any number of arguments of different data types, and returns a single object of the same data type. The c() function is a basic and frequently used function in R, and is an important part of working with vectors and lists.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic R interview β€” then scores it.
πŸ“ž Practice R β€” free 15 min
πŸ“• Buy this interview preparation book: 100 R questions & answers β€” PDF + EPUB for $5

All 100 R questions Β· All topics