Error: <text>:1:2: unexpected symbol
1: _x
^
The Husker Student POWER survey is currently open to first year and new transfer students.
The survey consists of 12 yes/no statements about academic, social, financial, and emotional well-being topics.
Staff and instructors with expertise use the survey responses to provide support and resources to students whose responses raise concerns.
Take a minute and complete the survey if you haven’t already!
Declare: set aside a space in memory for a variable
Assign: Set the value of a variable
In modern languages (R, Python, etc.) these two steps are usually done simultaneously
x <- 3
(R)
y = "hello world"
(Python)
.
(Python).
(Python)When you create a variable:
length_mm
c
in particular is a bad name in R!Use a function to test if the type of y
is xxx
R: is.xxx(y)
Python: isInstance(y, xxx)
Use a function to test if the type of y
is xxx
R: is.xxx(y)
Python: isinstance(y, xxx)
R and Python will both try to work with types that don’t match… to a point
Variables are converted to the least-complicated/least memory intensive compatible type.
R and Python will both try to work with types that don’t match… to a point
Variables are converted to the least-complicated/least memory intensive compatible type.
You can force things to be converted from one type to the other as well.
A function is a sequence of commands that performs a task
Functions (sometimes) take arguments and (sometimes) return values
We call a function by running code with the function name and values:
Function anatomy
A function is a sequence of commands that performs a task
Functions (sometimes) take arguments and (sometimes) return values
We call a function by running code with the function name and any necessary arguments
Function arguments
We often group commonly used, related functions into packages (R) or modules (Python)
You install a package with install.packages("pkgname")
(R) or pip install pkgname
(Python)
Packages/modules must be loaded before they can be used.
library(pkgname)
(R)
import pkgname
(Python)
In Python, you access the functions in a package by pkgname.funcname
, even after you’ve imported it
In R, after the library is loaded, you access a function using funcname
pkgname.funcname
can make for wordy code
import pkgname as pn
allows you to use pn.funcname
instead
This creates an alias for the package
from pkgname import func1 func2
allows you to use func1()
and func2()
directly, without the package name
This directly imports ONLY the functions you need
It is considered bad practice to use from pkgname import *
, which exposes all functions in the package directly.