From fcebc715d6469fa2b2b8439a694c1446053c68e0 Mon Sep 17 00:00:00 2001 From: January Weiner Date: Tue, 17 Sep 2024 15:11:07 +0200 Subject: [PATCH] updated L02 --- Lectures/lecture_02.html | 20 ++++++++++++++++++++ Lectures/lecture_02.rmd | 29 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/Lectures/lecture_02.html b/Lectures/lecture_02.html index 7b32760..86894d6 100644 --- a/Lectures/lecture_02.html +++ b/Lectures/lecture_02.html @@ -3527,6 +3527,16 @@

person$name
+

Creating and removing elements

+ +

You can add elements to a list using the $ operator:

+ +
person$city <- c("Berlin", "Hoppegarten")
+ +

You can remove elements by assigning the NULL value to them:

+ +
person$city <- NULL
+

Accessing lists

@@ -3570,6 +3580,16 @@

However, note that when you select a row, you will get a data frame, not a vector. This is because each of the column can be of different type, and vectors can hold only one type of data.

+

Creating new columns and removing columns

+ +

You can add new columns to a data frame using the $ operator:

+ +
d$city <- c("Hoppegarten", "Berlin", "Seattle")
+ +

You can remove columns by assigning the NULL value to them:

+ +
d$city <- NULL
+

Matrices vs data frames

Caveats:

diff --git a/Lectures/lecture_02.rmd b/Lectures/lecture_02.rmd index 04849db..130c918 100644 --- a/Lectures/lecture_02.rmd +++ b/Lectures/lecture_02.rmd @@ -312,6 +312,21 @@ There is a shortcut: person$name ``` +## Creating and removing elements + + +You can add elements to a list using the `$` operator: + +```{r eval=FALSE,echo=T} +person$city <- c("Berlin", "Hoppegarten") +``` + +You can remove elements by assigning the NULL value to them: + +```{r eval=FALSE,echo=T} +person$city <- NULL +``` + ## Accessing lists {.columns-2} ![](images/clothesline_2.png){ height=300px } @@ -364,6 +379,20 @@ However, note that when you select a row, you will get a data frame, not a vector. This is because each of the column can be of different type, and vectors can hold only one type of data. +## Creating new columns and removing columns + +You can add new columns to a data frame using the `$` operator: + +```{r} +d$city <- c("Hoppegarten", "Berlin", "Seattle") +``` + +You can remove columns by assigning the NULL value to them: + +```{r} +d$city <- NULL +``` + ## Matrices vs data frames Caveats: