Search notes:

R function: read.table

Reading text within the source file into a dataframe

With the text= parameter set, it's possible to read text that is stored within the source file itself into a data frame.
Note how read.table is clever enough to recognize the different data types of the data read.
df <- read.table(
  text='name val_1 val_2
foo 42 18.8
bar 123456789 -28.2
baz 0 99.9',
header = TRUE)

class(df)   # data.frame
mode(df)    # list
typeof(df)  # list

df
# 
#   name     val_1 val_2
# 1  foo        42  18.8
# 2  bar 123456789 -28.2
# 3  baz         0  99.9

class (df$name )  # factor
class (df$val_1)  # integer
class (df$val_2)  # numeric

mode  (df$name )  # numeric
mode  (df$val_1)  # numeric
mode  (df$val_2)  # numeric

typeof(df$name )  # integer
typeof(df$val_1)  # integer
typeof(df$val_2)  # double
Github repository about-r, path: /functions/read/table/text.R

Quoted strings

If the text to be read contains spaces, it can be quoted:
df <- read.table(
  text="name val_1
'item one' 1.1
'item two' 2.22
'item three' 3.333",
header = TRUE)

df
#
#         name val_1
# 1   item one 1.100
# 2   item two 2.220
# 3 item three 3.333
Github repository about-r, path: /functions/read/table/quoted.R

na.strings

The na.strings= argument specifies a string constant that should be interpreted as NA when reading the data.
df <- read.table(
  header     = TRUE ,          
  na.strings ='null',
  text       ='id val
1 foo
2 null
3 bar
4 baz'
)

df
# 
#   id  val
# 1  1  foo
# 2  2 <NA>
# 3  3  bar
# 4  4  baz
Github repository about-r, path: /functions/read/table/na.strings.R

Reading data from the internet

read.table recognizes an URL: Uniform Resource Locator and will fetch the data with HTTP.
#
#  https://stackoverflow.com/a/1296434/180275
#
url <- paste0(
       'https://random.org/integers/',
      '?num=100'                     , # Download 100 numbers
      '&base=10'                     , # with base 10
      '&min=0'                       , # in the range of 0 …
      '&max=100'                     , # 100
      '&col=2'                       , # Two columns
      '&format=plain'                , # text mode
      '&rnd=new'
    );

tab <- read.table(url);
Github repository about-r, path: /functions/read/table/url.R

See also

Import data into R for processing
Index to (some) R functions

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1745469490, '18.217.162.18', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/R/functions/read/table(147): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78