The return value of a function can be any R object. Although the return value is often a list, it could even be another function. You can transmit a value back to the caller by explicitly calling return() . Without this call, the value of the last executed statement will be returned by default. Matloff, Norman. Art of R Programming : A Tour of Statistical Software Design. San Francisco, CA, USA: No Starch Press, 2011. ProQuest ebrary. Web. 17 November 2014. Chapter 7: R Programming Structures 7.4 Return Values oddcount <- function(x) { k <- 0 for (n in x) { if (n %% 2 == 1) k <- k+1 } k # or use return(k) # the value of the last executed statement is returned # by the function if you do not explicitly include return # statement. } formals(oddcount) body(oddcount) saveRDS(df, "D:/R/myFirst.rds") df2 <- readRDS("D:/R/myFirst.rds") identical(df, df2) We can read the file all at once, like this: > z1 <- readLines("z1") > z1 [1] "John 25" "Mary 28" "Jim 19" Since each line is treated as a string, the return value here is a vector of strings---that is, a vector of character mode. There is one vector element for each line read, thus three elements here. Matloff, Norman. Art of R Programming : A Tour of Statistical Software Design. # extracts an integer field in the string s, in character positions # rng[1] through rng[2] intextract <- function(s, rng) { fld <- substr( s, rng[1], rng[2] ) return(as.integer(fld)) } Page 241 10.2.4 Extended Example: reading PUMS Census Files PUMS = Public Use Microdata Samples P U M S What does the term microdata here in PUMS mean? The term microdata here means that we are dealing with raw data and each record is for a real person, as opposed to statistical summaries. Data on many, many variables are included. The data is organized by household. For each unit, there is first a Household record, describing the various characteristics of that household, followed by one Person record for each person in the household. Character positions 106 and 107 (with numbering starting at 1) in the Household record state the number of Person records for that household. (The number can be very large, since some institutions count as households.) To enhance the integrity of the data, character position 1 contains H or P to confirm that this is a Household or Person record. Matloff, Norman. Art of R Programming : A Tour of Statistical Software Design exactlyone <- function(p) { notp <- 1 - p tot <- 0.0 for ( i in 1:length(p) ) tot <- tot + p[i] * prod( notp[-i] ) return(tot) } > df3 <- data.frame(a, b, c, stringsAsFactors=FALSE) > str(df3) 'data.frame': 10 obs. of 3 variables: $ a: int 1 2 3 4 5 6 7 8 9 10 $ b: num 3 5 7 9 11 13 15 17 19 21 $ c: chr "P" "A" "N" "T" ... > > > str(df) 'data.frame': 10 obs. of 3 variables: $ a: int 1 2 3 4 5 6 7 8 9 10 $ b: num 3 5 7 9 11 13 15 17 19 21 $ c: Factor w/ 10 levels "A","BALL","E",..: 6 1 5 9 4 3 7 8 10 2 > df3$c [1] "P" "A" "N" "T" "H" "E" "R" "S" "V" "BALL" > > > df$c [1] P A N T H E R S V BALL Levels: A BALL E H N P R S T V findwords <- function(tf) { # read in the words from the file, into a vector of mode character txt <- scan(tf, "") wl <- list() for ( i in 1:length(txt) ) { wrd <- txt[i] # ith word in input file wl[[wrd]] <- c(wl[[wrd]], i) } return(wl) } # sorts wrdlst, the output of findwords() alphabetically by word alphawl <- function( wrdlst ) { nms <- names(wrdlst) # the words sn <- sort(nms) # same words in alpha order return( wrdlst[sn] ) # return rearranged version } theFile <- "D:/R/data.txt" alphawl(findwords(theFile))