Skip to content

Commit

Permalink
new post
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed Nov 19, 2024
1 parent 55d28fb commit e6cc8cc
Show file tree
Hide file tree
Showing 9 changed files with 9,151 additions and 10,294 deletions.
4 changes: 2 additions & 2 deletions _freeze/posts/2024-11-19/index/execute-results/html.json

Large diffs are not rendered by default.

989 changes: 514 additions & 475 deletions docs/index.html

Large diffs are not rendered by default.

17,115 changes: 8,611 additions & 8,504 deletions docs/index.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/listings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
"listing": "/index.html",
"items": [
"/posts/2024-11-19/index.html",
"/posts/2024-11-18/index.html",
"/posts/2024-11-15/index.html",
"/posts/2024-11-14/index.html",
Expand Down
3 changes: 1 addition & 2 deletions docs/posts/2024-11-19/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
gtag('js', new Date());
gtag('config', 'G-JSJCM62KQJ', { 'anonymize_ip': true});
</script>
<meta name="quarto:status" content="draft">


<link rel="stylesheet" href="../../styles.css">
Expand All @@ -132,7 +131,7 @@
<body class="nav-fixed">

<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top quarto-banner"><div id="quarto-draft-alert" class="alert alert-warning"><i class="bi bi-pencil-square"></i>Draft</div>
<header id="quarto-header" class="headroom fixed-top quarto-banner">
<nav class="navbar navbar-expand-lg " data-bs-theme="dark">
<div class="navbar-container container-fluid">
<div class="navbar-brand-container mx-auto">
Expand Down
2 changes: 1 addition & 1 deletion docs/search.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/index.html</loc>
<lastmod>2022-11-16T15:17:41.340Z</lastmod>
<lastmod>2023-03-28T12:23:03.885Z</lastmod>
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/about.html</loc>
Expand Down Expand Up @@ -1902,6 +1902,6 @@
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/2024-11-19/index.html</loc>
<lastmod>2024-11-19T03:15:30.707Z</lastmod>
<lastmod>2024-11-19T13:00:41.976Z</lastmod>
</url>
</urlset>
37 changes: 19 additions & 18 deletions posts/2024-11-19/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ categories: [code, rtip, operations]
toc: TRUE
description: "Learn how to efficiently combine two or more vectors in R using base functions like c(), rbind(), cbind(), and data.frame(). Includes practical examples for R programmers."
keywords: [Programming, Combine vectors in R, R vector concatenation, Merge vectors in R, R vector combination, Combining R vectors, R c() function, R rbind() function, R cbind() function, R data frame from vectors, R vector recycling, How to combine two or more vectors in R, Combining vectors of different lengths in R, Best practices for combining vectors in R, Combining vectors into matrices in R, Creating data frames from multiple vectors in R]
draft: TRUE
---

# Introduction
Expand Down Expand Up @@ -101,7 +100,7 @@ print(combined)

## Type Coercion

R automatically coerces vector elements to a common type when combining vectors. The hierarchy is logical < integer < numeric < character.
R automatically coerces vector elements to a common type when combining vectors. The hierarchy is logical \< integer \< numeric \< character.

```{r}
# Combining different types
Expand All @@ -113,9 +112,9 @@ print(mixed_vec)

# Best Practices for Combining Vectors

1. **Check Vector Types**: Ensure vectors are of compatible types to avoid unexpected coercion.
2. **Verify Lengths**: Be mindful of vector lengths to prevent recycling issues.
3. **Use Meaningful Names**: Assign names to vector elements or data frame columns for clarity.
1. **Check Vector Types**: Ensure vectors are of compatible types to avoid unexpected coercion.
2. **Verify Lengths**: Be mindful of vector lengths to prevent recycling issues.
3. **Use Meaningful Names**: Assign names to vector elements or data frame columns for clarity.

# Practical Examples and Use Cases

Expand Down Expand Up @@ -153,15 +152,16 @@ print(ts_data)

Now that you've learned how to combine vectors in R, it's time to put your knowledge into practice. Try these exercises:

1. Create two numeric vectors of length 5 and combine them into a single vector.
2. Combine a character vector and a logical vector into a single vector. Observe the type coercion.
3. Create a 3x3 matrix by combining three vectors using `cbind()` and `rbind()`.
4. Combine two vectors of different lengths into a data frame and see how R recycles the shorter vector.
1. Create two numeric vectors of length 5 and combine them into a single vector.
2. Combine a character vector and a logical vector into a single vector. Observe the type coercion.
3. Create a 3x3 matrix by combining three vectors using `cbind()` and `rbind()`.
4. Combine two vectors of different lengths into a data frame and see how R recycles the shorter vector.

<details>

<summary>Click here for the solutions</summary>

1. Combining numeric vectors:
1. Combining numeric vectors:

```{r}
vec1 <- c(1, 2, 3, 4, 5)
Expand All @@ -170,7 +170,7 @@ combined <- c(vec1, vec2)
print(combined)
```

2. Combining character and logical vectors:
2. Combining character and logical vectors:

```{r}
char_vec <- c("a", "b", "c")
Expand All @@ -179,7 +179,7 @@ combined <- c(char_vec, logical_vec)
print(combined)
```

3. Creating a 3x3 matrix:
3. Creating a 3x3 matrix:

```{r}
vec1 <- c(1, 2, 3)
Expand All @@ -191,14 +191,15 @@ print(matrix_cbind)
print(matrix_rbind)
```

4. Combining vectors of different lengths into a data frame:
4. Combining vectors of different lengths into a data frame:

```{R}
short_vec <- c(1, 2)
long_vec <- c("a", "b", "c", "d")
df <- data.frame(Numbers = short_vec, Letters = long_vec)
print(df)
```

</details>

# Conclusion
Expand All @@ -207,11 +208,11 @@ Combining vectors in R is a crucial skill for data manipulation and analysis. By

# Quick Takeaways

- Use `c()` to combine vectors into a single vector
- Use `rbind()` and `cbind()` to create matrices from vectors
- Use `data.frame()` to convert vectors into a data frame
- Be aware of vector recycling when combining vectors of different lengths
- Coercion hierarchy: logical < integer < numeric < character
- Use `c()` to combine vectors into a single vector
- Use `rbind()` and `cbind()` to create matrices from vectors
- Use `data.frame()` to convert vectors into a data frame
- Be aware of vector recycling when combining vectors of different lengths
- Coercion hierarchy: logical \< integer \< numeric \< character

With this comprehensive guide and practical examples, you're now equipped with the knowledge to handle various vector combination tasks in R. Keep practicing these techniques to become a proficient R programmer!

Expand Down
Loading

0 comments on commit e6cc8cc

Please sign in to comment.