WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

R · Intermediate · question 34 of 100

What is the difference between seq() and rep() functions in R? Provide examples.?

📕 Buy this interview preparation book: 100 R questions & answers — PDF + EPUB for $5

In R, seq() and rep() are two functions that are used to create sequences of values. However, they have different functionalities and are used in different contexts.

The seq() function is used to create sequences of evenly spaced values. It takes three arguments: from, to, and by, which specify the starting value, the ending value, and the increment between values. Here’s an example:

    # Create a sequence of values from 1 to 10, incrementing by 2
    my_seq <- seq(from = 1, to = 10, by = 2)
    
    # Print the sequence
    print(my_seq)

In this example, we use the seq() function to create a sequence of values from 1 to 10, incrementing by 2. The resulting sequence is 1 3 5 7 9.

On the other hand, the rep() function is used to repeat values. It takes two arguments: x, which specifies the values to repeat, and times, which specifies the number of times to repeat those values. Here’s an example:

    # Repeat the values "a" and "b" three times each
    my_rep <- rep(c("a", "b"), times = 3)
    
    # Print the repetition
    print(my_rep)

In this example, we use the rep() function to repeat the values "a" and "b" three times each. The resulting repetition is "a" "b" "a" "b" "a" "b".

In summary, the seq() function is used to create sequences of evenly spaced values, while the rep() function is used to repeat values. While both functions can create sequences of values, they are used in different contexts and have different functionalities.

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