0% found this document useful (0 votes)
6 views6 pages

R Data Editing and Functions Guide

The document provides an overview of using the data editor in R for editing dataframes, including accessing it through the menu or command line. It also covers various mathematical and statistical functions available in R, as well as string manipulation techniques such as concatenation, formatting, and case conversion. Examples are provided for each function to illustrate their usage.

Uploaded by

gjob54530
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

R Data Editing and Functions Guide

The document provides an overview of using the data editor in R for editing dataframes, including accessing it through the menu or command line. It also covers various mathematical and statistical functions available in R, as well as string manipulation techniques such as concatenation, formatting, and case conversion. Examples are provided for each function to illustrate their usage.

Uploaded by

gjob54530
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Editor

There is a data editor within R that can be accessed from the menu bar by selecting Edit/Data
editor. You provide the name of the matrix or dataframe containing the material you want to edit
(this has to be a dataframe that is active in the current R session, rather than one which is stored
on file), and a Data Editor window appears. Alternatively, you can do this from the command
line using the fix function (e.g. fix([Link])). Suppose you want to edit the bacteria
dataframe which is part of the MASS library:

library(MASS)
attach(bacteria)
fix(bacteria)

The window has the look of an Excel spreadsheet, and you can change the contents of the cells,
navigating with the cursor or with the arrow keys. My preference is to do all of my data
preparation and data editing in Excel itself (because that is what it is good at). Once checked and
edited, I save the data from Excel to a tab-delimited text file (*.txt) that can be imported to R
very simply using the function called [Link]().

Arithmetic and Simple Functions

Math functions
R has an array of mathematical functions.

Operator Description
abs(x) Takes the absolute value of x
Takes the logarithm of x with base y; if base is not specified, returns the natural
log(x,base=y)
logarithm
exp(x) Returns the exponential of x
sqrt(x) Returns the square root of x
factorial(x) Returns the factorial of x (x!)
# sequence of number from 44 to 55 both including incremented by 1
x_vector <- seq(45,55, by = 1)
#logarithm
log(x_vector)
#exponential
exp(x_vector)
#squared root
sqrt(x_vector)
#factorial
factorial(x_vector)

Statistical functions
R standard installation contains wide range of statistical functions. In this tutorial, we will briefly
look at the most important function..

Basic statistic functions


Operator Description
mean(x) Mean of x
median(x) Median of x
var(x) Variance of x
sd(x) Standard deviation of x
summary(x) Summary of x: mean, min, max etc..
speed <- dt$speed
speed
# Mean speed of cars dataset
mean(speed)
median(speed)
var(speed)
# Standard deviation speed of cars dataset
sd(speed)

summary(speed)

R-String
Any value written within a pair of single quote or double quotes in R is treated as a string.
Internally R stores every string within double quotes, even when you create them with single
quote.

String Manipulation

Concatenating Strings - paste() function

Many strings in R are combined using the paste() function. It can take any number of
arguments to be combined together.
Syntax

The basic syntax for paste function is −


paste(..., sep = " ", collapse = NULL)
Following is the description of the parameters used −
 ... represents any number of arguments to be combined.
 sep represents any separator between the arguments. It is optional.
 collapse is used to eliminate the space in between two strings. But not the space within
two words of one string.

Example

a <- "Hello"
b <- 'How'
c <- "are you? "

print(paste(a,b,c))

print(paste(a,b,c, sep = "-"))

print(paste(a,b,c, sep = "", collapse = ""))


When we execute the above code, it produces the following result −
[1] "Hello How are you? "
[1] "Hello-How-are you? "
[1] "HelloHoware you? "

Formatting numbers & strings - format() function

Numbers and strings can be formatted to a specific style using format() function.

Syntax

The basic syntax for format function is −


format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))
Following is the description of the parameters used −
 x is the vector input.
 digits is the total number of digits displayed.
 nsmall is the minimum number of digits to the right of the decimal point.
 scientific is set to TRUE to display scientific notation.
 width indicates the minimum width to be displayed by padding blanks in the beginning.
 justify is the display of the string to left, right or center.

Example

# Total number of digits displayed. Last digit rounded off.


result <- format(23.123456789, digits = 9)
print(result)

# Display numbers in scientific notation.


result <- format(c(6, 13.14521), scientific = TRUE)
print(result)

# The minimum number of digits to the right of the decimal point.


result <- format(23.47, nsmall = 5)
print(result)

# Format treats everything as a string.


result <- format(6)
print(result)

# Numbers are padded with blank in the beginning for width.


result <- format(13.7, width = 6)
print(result)

# Left justify strings.


result <- format("Hello", width = 8, justify = "l")
print(result)

# Justfy string with center.


result <- format("Hello", width = 8, justify = "c")
print(result)
When we execute the above code, it produces the following result −
[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] " 13.7"
[1] "Hello "
[1] " Hello "

Counting number of characters in a string - nchar() function

This function counts the number of characters including spaces in a string.


Syntax

The basic syntax for nchar() function is −


nchar(x)
Following is the description of the parameters used −
 x is the vector input.

Example

result <- nchar("Count the number of characters")


print(result)
When we execute the above code, it produces the following result −
[1] 30

Changing the case - toupper() & tolower() functions

These functions change the case of characters of a string.

Syntax

The basic syntax for toupper() & tolower() function is −


toupper(x)
tolower(x)
Following is the description of the parameters used −
 x is the vector input.

Example

# Changing to Upper case.


result <- toupper("Changing To Upper")
print(result)

# Changing to lower case.


result <- tolower("Changing To Lower")
print(result)
When we execute the above code, it produces the following result −
[1] "CHANGING TO UPPER"
[1] "changing to lower"

Extracting parts of a string - substring() function


This function extracts parts of a String.

Syntax

The basic syntax for substring() function is −


substring(x,first,last)
Following is the description of the parameters used −
 x is the character vector input.
 first is the position of the first character to be extracted.
 last is the position of the last character to be extracted.

Example

# Extract characters from 5th to 7th position.


result <- substring("Extract", 5, 7)
print(result)
When we execute the above code, it produces the following result −
[1] "act"

You might also like