scan
and data.entry
commands# simmply assign a variable using the scan command with no arguments If you enter
# a blank line (just hit the Enter key)
x <- scan()
# 1: 28.8 2: 27.3 3: 45.8 4: 34.8 5: 23.5 6: Read 5 items
x
# [1] 28.8 27.3 45.8 34.8 23.5
# If you provide a filename, then the scan command will read the values from the
# file Suppose that we have a file called diameters.csv
y <- scan("data/diameters.csv")
y
# You can read more complex data from a file using the scan command, but you must
# specify the structure of the file. This means that you have to specify the data
# types. Here, assume that we have a data file called trees.csv:
x <- scan("data/trees.csv", what = list("character", "double"), sep = ",")
# The first column is the character data, and the second column is the numeric
# data.
print(x)
# [[1]] [1] 'pine' 'pine' 'oak' 'pine' 'oak'
# [[2]] [1] '28.8' '27.3' '45.8' '34.8' '25.3'
[[1]] [1] "pine" "pine" "oak" "pine" "oak" [[2]] [1] "28.8" "27.3" "45.8" "34.8" "25.3"
# Another method for entering data is to use the data.entry command. The command
# will open up a graphical interface x<- data.entry(1, 8, 4, 8, 9, 15,16)
using edit() or fix() function to edit the dataframe dynamically using interactive spreadsheet model
# not work in jupyter R-kernel
elements <- data.frame()
elements <- edit(elements) # Jupyter R kernel not supported this feature
elements <- fix(elements)
# edit() function displays the output result after edited but fix() function only saves results but not printed to the console
Load csv, xls, xlsx, sav, dta, por, sas and stata files by point and click features
Other similar features included: https://support.rstudio.com/hc/en-us/articles/218611977-Importing-Data-with-RStudio?mobile_site=true
* header
* sep
* stringsAsFactors
* row.names
* na.strings
* nrows
* skip
Comparison
# read.table(“clipboard”): It allows to copy data from Excel and read it directly
# in R. useful in some cases. myDf <- read.table('clipboard') myDf
#Using file.choose() argument (select yearly_sales.txt file)
# my_data <- read.table(file.choose(), sep=",")
# head(my_data)
# take R documentation help
# ?read.table or help(read.table)
# ??read.table or help.search("read.table")
# read.table and read.delim are intended to import other common file types such
# as .TXT
sales_table_txt <- read.table("data/yearly_sales.txt", header = TRUE, sep = ",")
head(sales_table_txt)
cust_id | sales_total | num_of_orders | gender |
---|---|---|---|
100001 | 800.64 | 3 | F |
100002 | 217.53 | 3 | F |
100003 | 74.58 | 2 | M |
100004 | 498.60 | 3 | M |
100005 | 723.11 | 4 | F |
100006 | 69.43 | 2 | F |
# Reading .dat files in R
trial <- read.table("data/trialTable.dat")
trial
names(trial)
names(trial)[names(trial)==names(trial)] <- c("one", "two", "three")
trial
V1 | V2 | V3 |
---|---|---|
1 | 2 | 3 |
3 | 5 | 6 |
one | two | three |
---|---|---|
1 | 2 | 3 |
3 | 5 | 6 |
iris <- read.table(url("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"),
header = FALSE, sep = ",")
# external sep argument is needed to import this file using read.table
head(iris)
V1 | V2 | V3 | V4 | V5 |
---|---|---|---|---|
5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa |
4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa |
4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa |
4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa |
5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa |
5.4 | 3.9 | 1.7 | 0.4 | Iris-setosa |
data <- read.table(url("https://raw.githubusercontent.com/mrtnj/rstuff/master/comb_gnome_data.csv"),
header = TRUE, sep = ",")
dim(data)
head(data)
group | treatment | mass0 | mass10 | mass25 | mass50 |
---|---|---|---|---|---|
green | control | 150.04841 | 188.1930 | 264.33913 | 465.6842 |
green | control | 128.94804 | 168.4952 | 251.67897 | 491.2235 |
green | pixies | 161.00249 | 267.2797 | 571.69731 | 2030.0171 |
green | pixies | 193.44995 | 267.8308 | 436.31328 | 984.0751 |
green | control | 101.88225 | 124.7450 | 169.00897 | 280.3632 |
green | control | 41.92258 | 51.2602 | 69.30728 | 114.5803 |
# Read in the data
df <- read.table("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/scores_timed.txt",
header = FALSE,
sep="/",
strip.white = TRUE,
na.strings = "EMPTY") # na.strings indicates which strings should be interpreted as NA values. In this case, the string “EMPTY” is to be interpreted as an NA value.
# Print out `df`
print(df)
V1 V2 V3 V4 V5 1 1 6 12:01:03 0.50 WORST 2 2 16 07:42:51 0.32 BEST 3 3 19 12:01:29 0.50 <NA> 4 4 13 03:22:50 0.14 INTERMEDIATE 5 5 8 09:30:03 0.40 WORST
# strip.white = TRUE feature
carSpeeds <- read.csv(file = "data/car-speeds.csv", stringsAsFactors = FALSE)
unique(carSpeeds$Color)
# we see two values for red cars.# Let’s try again, this time importing the data
# using the strip.white argument. NOTE - this argument must be accompanied by the
# sep argument, by which we indicate the type of delimiter in the file (the comma
# for most .csv files)
carSpeeds <- read.csv(file = "data/car-speeds.csv", stringsAsFactors = FALSE, strip.white = TRUE,
sep = ",")
unique(carSpeeds$Color)
trial <- read.csv("data/trialTable.csv", header = FALSE)
trial
V1 | V2 | V3 |
---|---|---|
1 | 2 | 3 |
3 | 5 | 6 |
# By default, strings in the data are converted to factors. If you load the data
# below with read.csv, then all the text columns will be treated as factors, even
# though it might make more sense to treat some of them as strings. To do this,
# use stringsAsFactors=FALSE:
data <- read.csv("data/datafile.csv")
str(data)
data <- read.csv("data/datafile.csv", stringsAsFactors = FALSE)
str(data)
# You might have to convert some columns to factors
data$Sex <- factor(data$Sex)
str(data)
'data.frame': 3 obs. of 4 variables: $ First : Factor w/ 3 levels "","Currer","Dr.": 2 3 1 $ Last : Factor w/ 3 levels "Bell","Seuss",..: 1 2 3 $ Sex : Factor w/ 2 levels "F","M": 1 2 NA $ Number: int 2 49 21 'data.frame': 3 obs. of 4 variables: $ First : chr "Currer" "Dr." "" $ Last : chr "Bell" "Seuss" "Student" $ Sex : chr "F" "M" NA $ Number: int 2 49 21 'data.frame': 3 obs. of 4 variables: $ First : chr "Currer" "Dr." "" $ Last : chr "Bell" "Seuss" "Student" $ Sex : Factor w/ 2 levels "F","M": 1 2 NA $ Number: int 2 49 21
# Another alternative is to load them as factors and convert some columns to characters:
data <- read.csv("data/datafile.csv")
str(data)
data$First <- as.character(data$First)
data$Last <- as.character(data$Last)
str(data)
# Another method: convert columns named "First" and "Last"
stringcols <- c("First","Last")
data[stringcols] <- lapply(data[stringcols], as.character)
str(data)
'data.frame': 3 obs. of 4 variables: $ First : Factor w/ 3 levels "","Currer","Dr.": 2 3 1 $ Last : Factor w/ 3 levels "Bell","Seuss",..: 1 2 3 $ Sex : Factor w/ 2 levels "F","M": 1 2 NA $ Number: int 2 49 21 'data.frame': 3 obs. of 4 variables: $ First : chr "Currer" "Dr." "" $ Last : chr "Bell" "Seuss" "Student" $ Sex : Factor w/ 2 levels "F","M": 1 2 NA $ Number: int 2 49 21 'data.frame': 3 obs. of 4 variables: $ First : chr "Currer" "Dr." "" $ Last : chr "Bell" "Seuss" "Student" $ Sex : Factor w/ 2 levels "F","M": 1 2 NA $ Number: int 2 49 21
# skipping unwanted rows
inventories <- read.csv("data/inventories.csv", skip = 6, header = TRUE, sep = ",")
head(inventories)
Line | X | X1994.1 | X1994.2 | X1994.3 | X1994.4 | X1995.1 | X1995.2 | X1995.3 | X1995.4 | X1996.1 | X1996.2 | X1996.3 | X1996.4 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | Manufacturing and trade | 918077 | 933394 | 943934 | 959179 | 974505 | 985513 | 992770 | 996357 | 997100 | 1001734 | 1009316 | 1016359 |
2 | Manufacturing | 396374 | 399508 | 402100 | 405833 | 411143 | 414963 | 418103 | 419897 | 424195 | 423339 | 426845 | 429994 |
3 | Durable goods | 237975 | 240652 | 242222 | 244930 | 248020 | 250316 | 253164 | 255633 | 259538 | 260090 | 263286 | 265147 |
4 | Lumber and wood products | 10264 | 10380 | 10159 | 10104 | 9967 | 9857 | 10303 | 10543 | 10559 | 10693 | 10696 | 10479 |
5 | Furniture and fixtures | 6695 | 6599 | 6647 | 6735 | 6810 | 6839 | 6753 | 6737 | 6501 | 6572 | 6650 | 6698 |
6 | Stone, clay, and glass products | 8313 | 8041 | 8114 | 8315 | 8475 | 8854 | 9023 | 8784 | 8844 | 8780 | 8844 | 9047 |
read.csv2()
variant used in countries that use a comma “,” as decimal point and a semicolon “;” as field separators.states_eu <- read.csv("data/states_eu.csv")
states_eu
str(states_eu)
states_eu <- read.csv2("data/states_eu.csv", stringsAsFactors=FALSE)
states_eu
state | capital | pop_mill | area_sqm | X |
---|---|---|---|---|
South Dakota | Pierre | 0,853 | 77116 | NA |
New York | Albany | 19,746 | 54555 | NA |
Oregon | Salem | 3,97 | 98381 | NA |
Vermont | Montpelier | 0,627 | 9616 | NA |
Hawaii | Honolulu | 1,42 | 10931 | NA |
'data.frame': 5 obs. of 5 variables: $ state : Factor w/ 5 levels "Hawaii","New York",..: 4 2 3 5 1 $ capital : Factor w/ 5 levels "Albany","Honolulu",..: 4 1 5 3 2 $ pop_mill: Factor w/ 5 levels "0,627","0,853",..: 2 4 5 1 3 $ area_sqm: int 77116 54555 98381 9616 10931 $ X : logi NA NA NA NA NA
state.capital.pop_mill.area_sqm. |
---|
South Dakota,Pierre,0,853,77116, |
New York,Albany,19,746,54555, |
Oregon,Salem,3,97,98381, |
Vermont,Montpelier,0,627,9616, |
Hawaii,Honolulu,1,42,10931, |
df <- read.csv2("data/testSemCol.csv", header = FALSE)
df
V1 | V2 | V3 |
---|---|---|
Col1 | Col2 | Col3 |
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
a | b | c |
df <- read.csv2("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/test.csv",
header = FALSE, sep=",")
df
V1 | V2 | V3 |
---|---|---|
Col1 | Col2 | Col3 |
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
a | b | c |
read.delim()
is almost the same as read.table(), except the field separator is tab by default. It is convenient for open tab delimited file.read.delim()
for reading “tab-separated value” files (“.txt”). By default, point (“.”) is used as decimal points.read.delim()
is related to other functions for reading data files into R, such as read.csv()
or read.table()
read.delim()
has arguments (or options) that are similar to the arguments of read.csv()
or read.table()
states<- read.delim("data/states.txt")
states<- read.delim("data/states.txt",stringsAsFactors=FALSE) # proper
states
state | capital | pop_mill | area_sqm |
---|---|---|---|
South Dakota | Pierre | 0.853 | 77116 |
New York | Albany | 19.746 | 54555 |
Oregon | Salem | 3.970 | 98381 |
Vermont | Montpelier | 0.627 | 9616 |
Hawaii | Honolulu | 1.420 | 10931 |
# filename <- 'http://www.nilu.no/projects/ccc/onlinedata/ozone/CZ03_2009.dat'
data <- read.delim("data/CZ03_2009.dat", header = TRUE, sep = "", skip = 2, as.is = TRUE)
head(data)
Date | Hour | Value |
---|---|---|
01.01.2009 | 00:00 | 34.3 |
01.01.2009 | 01:00 | 31.9 |
01.01.2009 | 02:00 | 29.9 |
01.01.2009 | 03:00 | 28.5 |
01.01.2009 | 04:00 | 32.9 |
01.01.2009 | 05:00 | 20.5 |
read.delim2()
for reading “tab-separated value” files (“.txt”). By default, comma (“,”) is used as decimal points.sales_delim2_txt <- read.delim2("data/yearly_sales.txt",sep = ",")
head(sales_delim2_txt)
cust_id | sales_total | num_of_orders | gender |
---|---|---|---|
100001 | 800.64 | 3 | F |
100002 | 217.53 | 3 | F |
100003 | 74.58 | 2 | M |
100004 | 498.6 | 3 | M |
100005 | 723.11 | 4 | F |
100006 | 69.43 | 2 | F |
# The first three columns are assumed to contain letters, the next five columns
# contain numbers, the next two columns have letters, and the last four columns
# are numbers
trial <- read.fwf("data/trialFWF.dat", c(3, 5, 2, 4), skip = 1)
trial
trial$V1
V1 | V2 | V3 | V4 |
---|---|---|---|
B | 100 | ZZ | 18 |
C | 200 | YY | 20 |
D | 300 | XX | 22 |
# column can be ignored by giving a negative number
trial <- read.fwf("data/trialFWF.dat", c(3, -5, 2, 4), skip = 1)
trial
V1 | V2 | V3 |
---|---|---|
B | ZZ | 18 |
C | YY | 20 |
D | XX | 22 |
library(readr)
# Import potatoes.txt using read_delim(): potatoes
potatoesTXT <- read_delim("data/potatoes.txt", delim = "\t")
# Print out potatoes
potatoesTXT
#read_csv function (readr package)
# readr is already loaded
# Column names
properties <- c("area", "temp", "size", "storage", "method",
"texture", "flavor", "moistness")
# Import potatoes.csv with read_csv(): potatoes
potatoesCSV <- read_csv("data/potatoes.csv", col_names = properties)
potatoesCSV
Parsed with column specification: cols( area = col_integer(), temp = col_integer(), size = col_integer(), storage = col_integer(), method = col_integer(), texture = col_double(), flavor = col_double(), moistness = col_double() )
area | temp | size | storage | method | texture | flavor | moistness |
---|---|---|---|---|---|---|---|
1 | 1 | 1 | 1 | 1 | 2.9 | 3.2 | 3.0 |
1 | 1 | 1 | 1 | 2 | 2.3 | 2.5 | 2.6 |
1 | 1 | 1 | 1 | 3 | 2.5 | 2.8 | 2.8 |
1 | 1 | 1 | 1 | 4 | 2.1 | 2.9 | 2.4 |
1 | 1 | 1 | 1 | 5 | 1.9 | 2.8 | 2.2 |
1 | 1 | 1 | 2 | 1 | 1.8 | 3.0 | 1.7 |
1 | 1 | 1 | 2 | 2 | 2.6 | 3.1 | 2.4 |
1 | 1 | 1 | 2 | 3 | 3.0 | 3.0 | 2.9 |
1 | 1 | 1 | 2 | 4 | 2.2 | 3.2 | 2.5 |
1 | 1 | 1 | 2 | 5 | 2.0 | 2.8 | 1.9 |
1 | 1 | 1 | 3 | 1 | 1.8 | 2.6 | 1.5 |
1 | 1 | 1 | 3 | 2 | 2.0 | 2.8 | 1.9 |
1 | 1 | 1 | 3 | 3 | 2.6 | 2.6 | 2.6 |
1 | 1 | 1 | 3 | 4 | 2.1 | 3.2 | 2.1 |
1 | 1 | 1 | 3 | 5 | 2.5 | 3.0 | 2.1 |
1 | 1 | 1 | 4 | 1 | 2.6 | 3.1 | 2.4 |
1 | 1 | 1 | 4 | 2 | 2.7 | 2.9 | 2.4 |
1 | 1 | 1 | 4 | 3 | 2.2 | 3.1 | 2.3 |
1 | 1 | 1 | 4 | 4 | 3.1 | 3.4 | 2.7 |
1 | 1 | 1 | 4 | 5 | 3.0 | 2.6 | 2.7 |
1 | 1 | 2 | 1 | 1 | 3.1 | 3.0 | 2.8 |
1 | 1 | 2 | 1 | 2 | 2.7 | 2.8 | 2.7 |
1 | 1 | 2 | 1 | 3 | 2.4 | 3.0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 2 | 2 | 2 | 5 | 2.8 | 2.7 | 2.6 |
2 | 2 | 2 | 3 | 1 | 2.5 | 3.2 | 2.3 |
2 | 2 | 2 | 3 | 2 | 2.9 | 3.3 | 2.7 |
2 | 2 | 2 | 3 | 3 | 2.5 | 3.1 | 2.5 |
2 | 2 | 2 | 3 | 4 | 3.0 | 2.9 | 2.5 |
2 | 2 | 2 | 3 | 5 | 2.9 | 3.1 | 3.1 |
2 | 2 | 2 | 4 | 1 | 2.7 | 3.3 | 2.6 |
2 | 2 | 2 | 4 | 2 | 2.6 | 2.8 | 2.3 |
2 | 2 | 2 | 4 | 3 | 2.5 | 3.1 | 2.6 |
2 | 2 | 2 | 4 | 4 | 3.4 | 3.3 | 3.0 |
2 | 2 | 2 | 4 | 5 | 2.5 | 2.8 | 2.3 |
Parsed with column specification: cols( area = col_integer(), temp = col_integer(), size = col_integer(), storage = col_integer(), method = col_integer(), texture = col_double(), flavor = col_double(), moistness = col_double() )
area | temp | size | storage | method | texture | flavor | moistness |
---|---|---|---|---|---|---|---|
1 | 1 | 1 | 1 | 1 | 2.9 | 3.2 | 3.0 |
1 | 1 | 1 | 1 | 2 | 2.3 | 2.5 | 2.6 |
1 | 1 | 1 | 1 | 3 | 2.5 | 2.8 | 2.8 |
1 | 1 | 1 | 1 | 4 | 2.1 | 2.9 | 2.4 |
1 | 1 | 1 | 1 | 5 | 1.9 | 2.8 | 2.2 |
1 | 1 | 1 | 2 | 1 | 1.8 | 3.0 | 1.7 |
1 | 1 | 1 | 2 | 2 | 2.6 | 3.1 | 2.4 |
1 | 1 | 1 | 2 | 3 | 3.0 | 3.0 | 2.9 |
1 | 1 | 1 | 2 | 4 | 2.2 | 3.2 | 2.5 |
1 | 1 | 1 | 2 | 5 | 2.0 | 2.8 | 1.9 |
1 | 1 | 1 | 3 | 1 | 1.8 | 2.6 | 1.5 |
1 | 1 | 1 | 3 | 2 | 2.0 | 2.8 | 1.9 |
1 | 1 | 1 | 3 | 3 | 2.6 | 2.6 |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 | 2 | 1 | 1 | 2.7 | 2.5 | 2.5 |
2 | 2 | 2 | 1 | 2 | 2.5 | 2.7 | 2.3 |
2 | 2 | 2 | 1 | 3 | 3.2 | 2.7 | 3.0 |
2 | 2 | 2 | 1 | 4 | 2.4 | 2.7 | 2.5 |
2 | 2 | 2 | 1 | 5 | 2.7 | 2.1 | 2.3 |
2 | 2 | 2 | 2 | 1 | 2.2 | 2.7 | 2.3 |
2 | 2 | 2 | 2 | 2 | 3.1 | 2.9 | 2.6 |
2 | 2 | 2 | 2 | 3 | 2.2 | 2.8 | 3.1 |
2 | 2 | 2 | 2 | 4 | 2.9 | 3.0 | 2.7 |
2 | 2 | 2 | 2 | 5 | 2.8 | 2.7 | 2.6 |
2 | 2 | 2 | 3 | 1 | 2.5 | 3.2 | 2.3 |
2 | 2 | 2 | 3 | 2 | 2.9 | 3.3 | 2.7 |
2 | 2 | 2 | 3 | 3 | 2.5 | 3.1 | 2.5 |
2 | 2 | 2 | 3 | 4 | 3.0 | 2.9 | 2.5 |
2 | 2 | 2 | 3 | 5 | 2.9 | 3.1 | 3.1 |
2 | 2 | 2 | 4 | 1 | 2.7 | 3.3 | 2.6 |
2 | 2 | 2 | 4 | 2 | 2.6 | 2.8 | 2.3 |
2 | 2 | 2 | 4 | 3 | 2.5 | 3.1 | 2.6 |
2 | 2 | 2 | 4 | 4 | 3.4 | 3.3 | 3.0 |
2 | 2 | 2 | 4 | 5 | 2.5 | 2.8 | 2.3 |
15 | 16 | 25 | 87 | 5 | 6.5 | 8.7 | 4.5 |
Further References: https://blog.rstudio.com/2016/08/05/readr-1-0-0/
Modifying changes in imported file and write/export those changes to the disk
sales <- read.csv("data/yearly_sales.csv")
sales$per_order <- sales$sales_total/sales$num_of_orders
write.table(sales, "data/sales_modified.txt", sep = "\t", row.names = FALSE) # row.names argument option
month <- month.abb
avgHigh <- c(38, 41, 47, 56, 69, 81, 83, 82, 71, 55, 48, 43)
seasons <- c("Winter", "Spring", "Summer", "Fall")
season <- rep(seasons[c(1:4,1)], c(2,3,3,3,1))
schoolIn <- rep(c("yes", "no", "yes"), c(5, 3, 4))
getwd()
d <- data.frame(month, avgHigh, season, schoolIn)
write.table(d, "data/annual.txt", quote=TRUE,
sep="\t", row.names=FALSE)
d <- read.delim("data/annual.txt", header=TRUE, sep="\t")
d
month | avgHigh | season | schoolIn |
---|---|---|---|
Jan | 38 | Winter | yes |
Feb | 41 | Winter | yes |
Mar | 47 | Spring | yes |
Apr | 56 | Spring | yes |
May | 69 | Spring | yes |
Jun | 81 | Summer | no |
Jul | 83 | Summer | no |
Aug | 82 | Summer | no |
Sep | 71 | Fall | yes |
Oct | 55 | Fall | yes |
Nov | 48 | Fall | yes |
Dec | 43 | Winter | yes |
#quote argument option
t=data.frame(rep("a",5), rep("b",5))
t
write.table(t,"data/try.txt",row.names=F,col.names=F,sep="\t")
write.table(t,"data/tryN.txt",row.names=F,col.names=F,sep="\t", quote=FALSE)
rep..a...5. | rep..b...5. |
---|---|
a | b |
a | b |
a | b |
a | b |
a | b |
Import Data into R: Read CSV, Excel, SPSS, Stata, SAS Files: https://www.guru99.com/r-import-data.html
Exporting Data to Excel, CSV, SAS, STATA, Text File: https://www.guru99.com/r-exporting-data.html