Monthly Archives: February 2018

Spring 2018 R Open Lab: Character Strings

Feb 28

For this week, the topic we discussed is character strings in R. This lab’s content is a steppingstone for text  analysis.  We started with introducing the concept of characters, character strings, and character string vectors in R. Then, we talked about operations on strings such as getting substring and combining different character strings.  Finally, we learned about extracting and replacing certain patterns within a text-form data set.

Here is the link to the script for this open lab:

https://drive.google.com/file/d/12Etf6qQtpJPymIYrGRqLiaxt8LchNBUB/view?usp=sharing

Here is a reference for regular expressions in R:

https://www.rstudio.com/wp-content/uploads/2016/09/RegExCheatsheet.pdf

Thank you all for showing up. If you have further question regarding topics covered in the material, please feel free to drop by during next week’s lab or email me or leave a comment.

See you all next week!

Python Open Lab Week 3: Feb 26

For week three we decided to tackle strings, string methods, and functions:

String Methods

Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable.

The string data type has multiple methods. Here are all of the methods of list objects:

Examples:

str.upper()  – Making Strings uppercase

str.lower() – Making Strings lowercase

str.join() method will concatenate two strings, but in a way that passes one string through another.

str.split() method returns a list of strings that are separated by whitespace if no other parameter is given.

str.replace() method can take an original string and return an updated string with some replacement.

Boolean methods:

Method True if
str.isalnum() If string consists of only alpha-numeric values
str.isalpha() If string consists of only alphabets
str.islower() If string consists of only lower-case  values
str.isnumeric() String consists of only numeric characters
str.isspace() String consists of only whitespace characters
str.istitle() String is in title case
str.isupper() String’s alphabetic characters are all upper case

String comparison

You can use ( > , < , <= , <= , == , !=  ) to compare two strings. Python compares string lexicographically i.e using ASCII value of the characters.

Suppose you have str1  as “Mary”  and str2  as “Mac” . The first two characters fromstr1  and str2 ( M  and M ) are compared. As they are equal, the second two characters are compared. Because they are also equal, the third two characters ( r  and c ) are compared. And because ‘r’  has greater ASCII value than ‘c’ , str1  is greater than str2 .

Functions:

You use functions in programming to bundle a set of instructions that you want to use repeatedly or that, because of their complexity, are better self-contained in a sub-program and called when needed. That means that a function is a piece of code written to carry out a specified task. To carry out that specific task, the function might or might not need multiple inputs. When the task is carried out, the function can or can not return one or more values.

There are three types of functions in Python:

  • Built-in functions, such as help() to ask for help, min() to get the minimum value, print() to print an object to the terminal
  • User-Defined Functions (UDFs), which are functions that users create to help them out
  • Anonymous functions, which are also called lambda functions because they are not declared with the standard def keyword.

Functions vs Methods:

A method refers to a function which is part of a class. You access it with an instance or object of the class. A function doesn’t have this restriction: it just refers to a standalone function. This means that all methods are functions but not all functions are methods.

 

Spring 2018 R Open Lab: More Fundamentals

Last week, we walked through the R starter kit which introduced most of the useful basic concepts in R such as vectors, matrices, and loops. This week, we continued to talk about more basics in R and demonstrated examples. The goal of this lab is to get attendants a better understanding of how R language works so that they can transform their specific real-life problem into R algorithms smoothly.

Here is the link to the script for this open lab:

https://drive.google.com/file/d/1SePSSVF980EJfCxv4eZ4AQlCP7vlTbe7/view?usp=sharing

The script also has comments and explanations. You can open it with R studio and run it step by step.

Thank you all for showing up. If you have further question regarding topics covered in the material, please feel free to drop by during next week’s lab or email me or leave a comment. See you all next week!

Python Open Lab Feb 19

On week two of the open labs, we continued building on the starter kit and worked on python data structures. We discussed loops (both for and while), dictionaries and dictionary methods.

A while loop is a concept that, when implemented, executes a piece of code over and over again, while a given condition remains true:

When constructing a while loop, you have to ensure that it has all three of the following elements:

  1. The while keyword
  2. A condition that translates to either true or false
  3. A block of code you want to repeat

Example:

# Input/pick a random number

number = 2

# Set the condition of the while loop

while number < 5 :

print(“Thank you”)

# Increment the value of the variable “number by 1”

number = number+1

When you run this code you produce the following result:

Thank you

Thank you

Thank you

 

For loop:

The for loop is used to iterate over elements of a sequence, it is often used when you have a piece of code which you want to repeat “x” number of times:

Example:

Let’s say you have a list:

fresh_fruits = [“Apple”, “Banana”, “Peach”, “Strawberry”]

for fruits in fresh_fruits:

print fruits

That means, for every element that we assign the variable fruits, in the list fresh_fruits, print out the variable fruits.

  1. Dictionaries

Dictionaries are similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – tare similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – they aren’t in any specific order, either – the key does the same thing. You can add, remove, and modify the values in dictionaries. Example: telephone book.

In the Python dictionary, each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.

An empty dictionary without any items is written with just two curly braces, like this: {}. Keys within a dictionary must be a data type such as strings, numbers, or tuples.

Example:

dict = {‘Name’: ‘Michael’, ‘Age’: 7, ‘Class’: ‘First’}

print “dict[‘Name’]: “, dict[‘Name’]
print “dict[‘Age’]: “, dict[‘Age’]

When we run this code, it produces the following result:

dict[‘Name’]:  Michael
dict[‘Age’]:  7

Dictionary Methods:

  1. clear(): Removes all items from the dictionary in place
  2. copy(): Returns a new dictionary with the same contents
  3. fromkeys(): creates a new dictionary from the given keys
  4. get(): A way to access dictionary elements. Returns None when element not present in the dictionary.
  5. has_key():  boolean method to check if a key is present in the dictionary
  6. pop(): returns value corresponding to the key and also removes the pair from the dictionary
  7. popitem(): pops out a random item from the dictionary