R for C Developers
Move from C to base R: vectors, missing values, data frames, functions, S3 dispatch, S4 validity and closure state. Build a connected multi-file basket and port a stock ledger.
C and R side by side
The same program in C and in R, from the lessons of this course.
Text and Output
C printf interprets a format string and puts adds a newline. R cat writes values without an automatic final newline, while paste and paste0 build strings. Rscript executes an entry file, and source evaluates supporting declarations. Keep output explicit…
C1#include <stdio.h>2 3int main(void)4{5 // Print one greeting.6 printf("%s%s\n", "Hi", ", Ada");7 return 0;8}R1ready <- 32cat(paste0("Ready: ", ready), "\n", sep="")Comparisons and Recycling
C scalar comparisons produce integer truth values, while strings need content comparison such as strcmp rather than pointer equality. R == is vectorized and can recycle shorter operands. all can reduce comparisons to one logical result, while identical…
C1#include <stdio.h>2#include <string.h>3 4int main(void){5char left[]={'t','e','a','\0'};printf("%d\n",strcmp(left,"tea")==0);6return 0;7}R1cat(identical(1L, 1), all(c(2, 3) == c(2, 3)), "\n")Vector Subsetting
C arrays use zero-based offsets, and negative subscripts are unsafe unless valid pointer positioning makes them refer within the same array. R positive indexes are one-based, zero selects nothing and negative indexes exclude positions. Do not mix positive and…
C1#include <stdio.h>2 3int main(void){4int values[]={4,8,12};size_t count=sizeof values/sizeof values[0];printf("%d\n",values[count-1]);5return 0;6}R1values <- c(4, 8, 12)2cat(paste(values[-1], collapse=" "), "\n", sep="")Typed Iteration
C loops write results into explicitly typed storage, with caller-managed lengths and allocation. R lapply returns a list, sapply attempts simplification and vapply requires a prototype for each result’s type and length. A declared result shape makes…
C1#include <stdio.h>2#include <string.h>3 4int main(void){5const char *words[]={"a","tea"};size_t lengths[2];for(size_t i=0;i<2;++i)lengths[i]=strlen(words[i]);printf("%zu:%zu\n",lengths[0],lengths[1]);6return 0;7}R1words <- c("a", "tea")2sizes <- vapply(words, nchar, integer(1), USE.NAMES=FALSE)3cat(paste(sizes, collapse=" "), "\n", sep="")Functions and Returns
C functions use declared signatures and explicit return statements for results, with eager argument evaluation. R returns the last evaluated expression by default and offers return for early exits. R calls use lexical scoping and lazy argument promises.…
C1#include <stdio.h>2static int twice(int x) {3 return x * 2;4}5int main(void) {6 printf("%d\n", twice(4));7 return 0;8}R1is_long <- function(text) nchar(text)>42cat(is_long("coffee"), "\n", sep="")String Parsing
C text parsing commonly combines string scanning, tokenization and conversions such as strtol, whose end pointer detects trailing text. R trimws and strsplit operate on character vectors, with strsplit returning a list. Fixed separators and regular…
C1#include <stdio.h>2#include <stdlib.h>3#include <errno.h>4 5int main(void) {6const char *text="12cats";char *end;errno=0;long value=strtol(text,&end,10);(void)value;puts(end!=text && *end=='\0' && errno!=ERANGE ? "valid":"invalid");7return 0;8}R1parts <- strsplit("tea,cake", ",", fixed=TRUE)[[1]]2cat(length(parts), "\n", sep="")
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsFiles and Atomic Values
6 lessonsLogic and Iteration
6 lessonsCollections and Ownership
5 lessonsData and Transformations
4 lessonsFunctions and Closures
5 lessonsParsing and S3 Dispatch
5 lessonsObjects and Invariants
6 lessonsCollaboration and Contracts
5 lessonsProject: Basket
4 lessonsFinal Challenges
2 lessonsFrequently asked questions
Who is R for C Developers for?
For people who already write C and want to learn R. Each lesson starts from C you know and shows how R does the same thing, so you skip the beginner basics.
Do I need to know R already?
No. The course starts with your first R program. You need C basics: variables, conditions, loops, functions and collections.
How many lessons does R for C Developers have?
R for C Developers contains 48 interactive lessons you can complete at your own pace.
What does R for C Developers cover?
The course is organized into chapters covering: Files and Atomic Values, Logic and Iteration, Collections and Ownership, Data and Transformations, and more.
Is R for C Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.