.chapter8<-function(i=0){ " i Chapter 8: Simple data manipulation i Explanations - ------------------------------------- -- ---------------------- 1 head() and tail() functions 21 SEC index: website 2 summary() 22 : ruler.R 3 ls() vs. ls(pattern='my-pattern') 23 : calling ruler() 4 typeof(), is.vector(), functions with 'is.' 24 : 100 lines 5 length() vs. dim() 25 : read.fwf() 6 cbind(), and recycling rule 26 Merge: merge() function 7 Converting a vector to a matrix 27 : by one common variable 8 round() function 28 : by different names 9 colnames() and rownames() functions 29 : 3 data sets 10 Getting specific columns/rows from a matrix 30 : inner-,left-,right-,all-join 11 Retrieving a subset 31 : library(dplyr) 12 Converting a list into a matrix 32 tibble 13 sort() and order() 33 : examples 14 choose : columns: two ways 34 factor 15 : rows 35 : convert factors to values 16 : 1927 year's data 36 dplyr package 17 apply() 37 : %>% and group_by() 18 subset 38 :filter() 19 Videos 39 :select() 20 Links 40 :select(start_with()) Example #1:>.c8 # get the above list Example #2:>.c8() # the same as the above Example #3:>.c8(1) # see the first explanation ";.zchapter8(i)} .n8chapter<-40 .zchapter8<-function(i){ if(i==0){ print(.c8) }else{ .printEachQ(8,i,.n8chapter) } } .c8<-.chapter8 .C8EXPLAIN1<-"head() and tail() function /////////////////////// head(x) head(x,2) tail(x) tail(x,5) > .search(\"ff3Month\") DIR NAME 22 data_csv ff3Monthly.csv 52 data_excel ff3Monthly.xlsx 279 data_R ff3monthly.RData 280 data_R ff3MonthlyCRSP.RData 583 data_sas ff3Monthly.sas7bdat 584 data_sas ff3Monthly.sas7bndx 756 data_txt ff3Monthly.txt > .download2dotx(22) # File name is ff3Monthly.csv # Check data set called .x > head(.x) DATE MKT_RF SMB HML RF 1 1926-09-01 0.0036 -0.0132 0.0001 0.0023 2 1926-10-01 -0.0324 0.0004 0.0051 0.0032 3 1926-11-01 0.0253 -0.0020 -0.0035 0.0031 4 1926-12-01 0.0262 -0.0004 -0.0002 0.0028 5 1927-01-01 -0.0006 -0.0056 0.0483 0.0025 6 1927-02-01 0.0418 -0.0010 0.0317 0.0026 /////////////////////// " .C8EXPLAIN2<-"summary() function /////////////////////// # -- Example 2 ----------------- x<-seq(1,500,1.5) summary(x) Min. 1st Qu. Median Mean 3rd Qu. Max. 1.0 125.5 250.0 250.0 374.5 499.0 # -- Example 2 ----------------- path<-\"http://datayyy.com/data_R/\" file<-\"ff3daily.RData\" infile<-paste0(path,file) a<-load(url(infile)) summary(.x) /////////////////////// " .C8EXPLAIN3<-"ls() vs. ls(pattern='my-pattern') /////////////////////// ls() # show all available variables and functions # i.e., show all objects ls(pattern=\"pv\") # show all objects containing 'pv' ls(pattern=\"^pv\") # show all objects starting with 'pv' /////////////////////// " .C8EXPLAIN4<-"typeof(), is.vector() /////////////////////// > typeof(10) [1] \"double\" > typeof('abc') [1] \"character\" To get all embedded functions starting with 'is.', we issue the following code apropos(\"is.\") /////////////////////// " .C8EXPLAIN5<-"length() vs. dim() /////////////////////// The length() function is for a vector The dim() function is for a matrix > x<-1:100 > y<-matrix(x,2,50) > length(x) [1] 100 > dim(x) NULL > length(y) [1] 100 > dim(y) [1] 2 50 /////////////////////// " .C8EXPLAIN6<-"cbind() /////////////////////// The cbind() can be sed to combine columns. > x<-1:100 > x<-1:10 > y<-20:11 > z<-cbind(x,y) > head(z) x y [1,] 1 20 [2,] 2 19 [3,] 3 18 [4,] 4 17 [5,] 5 16 [6,] 6 15 Recycling rule > x<-1:10 > y<-1:5 > z<-cbind(x,y) > z x y [1,] 1 1 [2,] 2 2 [3,] 3 3 [4,] 4 4 [5,] 5 5 [6,] 6 1 [7,] 7 2 [8,] 8 3 [9,] 9 4 [10,] 10 5 /////////////////////// " .C8EXPLAIN7<-"Converting a vector to a matrix /////////////////////// x<-1:100 y<-matrix(x,2,50) /////////////////////// " .C8EXPLAIN8<-"rownames() vs. colnames() functions /////////////////////// /////////////////////// " .C8EXPLAIN9<-"The round() function /////////////////////// After applying the round() or roundup() functions, our numbers will become more readable. input 1: our value input 2: number of digits =ROUND(1.2222,2) -> 1.22 =ROUND(1.2252,2) -> 1.23 =ROUNDUP(1.1444,2) -> 1.15 =ROUNDDOWN(1.1444,2) -> 1.14 /////////////////////// " .C8EXPLAIN10<-"Getting specific columns or rows from a matrix /////////////////////// Assume that we have a matrix of x Getting a value x[2,3] Getting a column x[,2] Getting a row x[2,] Getting a few columns x[m2:10] Getting a few rows x[1:10,] Using name x$DATE (asume that the first column is DATE) Negative value means delete x[,-2] except the second column /////////////////////// " .C8EXPLAIN11<-"Retrieving a subset /////////////////////// Method I: y<-x[,2] Method II: y<-data.frame(x[,1],x[10]) Method III: y<-data.frame(x[,-2]) Method IV: using subset() function /////////////////////// " .C8EXPLAIN12<-"Converting a list into a matrix /////////////////////// We can try various ways to achieve this objective. unlist() function as.matrix() function matrix() function /////////////////////// " .C8EXPLAIN13<-"sort() /////////////////////// x<-c(4,1,3,-9) x2<-sort(x) print(x2) [1] -9 1 3 4 /////////////////////// " .C8EXPLAIN14<-"choose columns /////////////////////// path<-\"http://datayyy.com/data_R/\" file<-\"ff3daily.RData\" infile<-paste0(path,file) a<-load(url(infile)) print(a) # [1] \".x\" > head(.x,2) DATE MKT_RF SMB HML RF 1 1926-07-01 0.0010 -0.0025 -0.0027 9e-05 2 1926-07-02 0.0045 -0.0033 -0.0006 9e-05 excessMktRet<-.x$MKT_RF excessMktRet2<-.x[,2] /////////////////////// " .C8EXPLAIN15<-" /////////////////////// /////////////////////// " .C8EXPLAIN16<-" /////////////////////// path<-\"http://datayyy.com/data_R/\" file<-\"ff3monthly.RData\" infile<-paste0(path,file) a<-load(url(infile)) print(a) print(head(.x)) begdate<-as.Date(\"1927-1-1\") enddate<-as.Date(\"1977-3-31\") b<-.x[as.Date(.x$DATE)>=begdate & as.Date(.x$DATE)<=enddate,] print(b) > print(b) DATE MKT_RF SMB HML RF 607 1977-01-01 -0.0405 0.0476 0.0427 0.0036 608 1977-02-01 -0.0194 0.0104 0.0047 0.0035 609 1977-03-01 -0.0137 0.0100 0.0109 0.0038 /////////////////////// " .C8EXPLAIN17<-"apply() function /////////////////////// x <- cbind(x1 = 3, x2 = c(4:1, 2:5)) print(x) x1 x2 [1,] 3 4 [2,] 3 3 [3,] 3 2 [4,] 3 1 [5,] 3 2 [6,] 3 3 [7,] 3 4 [8,] 3 5 apply(x, 1, mean) # 1 for rows [1] 3.5 3.0 2.5 2.0 2.5 3.0 3.5 4.0 apply(x, 2, mean, trim = .2) # 2 for columns x1 x2 3 3 > mean(x) # for all values /////////////////////// " .C8EXPLAIN18<-"subset() function /////////////////////// x<- data.frame( Name = c(\"Orange\", \"Apple\", \"Lemon\", \"Banana\", \"Grapefruit\", \"Strawberry\"), Type=c(\"Citrus\",\"Pome\",\"Citrus\",\"Berry\",\"Citrus\", \"Berry\"), Price = c(1.00, 1.50, 0.75, 0.50, 1.25, 2.00) ) cheap_fruits <- subset(df, Price < 1.00) print(cheap_fruits) /////////////////////// " .C8EXPLAIN19<-"Youtubes /////////////////////// Malakar,Gopal,2017, How to Merge Data Sets (join) in R (v20k,s7k,t3:04) https://www.youtube.com/watch?v=RNmgnVvcfU4 GordonAnthonyDavis,R Programming Tutorial Lesson 16: Merging Data Frames (v35k,s4k,t7:03) https://www.youtube.com/watch?v=rkmIASiE8iI /////////////////////// " .C8EXPLAIN20<-"Links /////////////////////// Bhalla, Deepanshu,2017, Exclusive Tutorial on Data Manipulation with R (50 Examples) https://www.datasciencecentral.com/profiles/blogs/exclusive-tutorial-on-data-manipulation-with-r-50-examples Data Management in R,2017, Best packages for data manipulation in R https://datascienceplus.com/best-packages-for-data-manipulation-in-r/ R document, factor https://www.rdocumentation.org/packages/base/versions/3.6.1/topics/factor SEC index website https://www.sec.gov/Archives/edgar/full-index/ /////////////////////// " .C8EXPLAIN21<-"SEC index website /////////////////////// Step 1: at the following website https://www.sec.gov/Archives/edgar/full-index/ Step 2: download the first quarter in 2024 index file called company.idx Step 3: run the following code infile<-\"c://temp/company.idx\" x<-readLines(infile,n=100) print(head(x,20)) /////////////////////// " .C8EXPLAIN22<-"ruler() function /////////////////////// .ruler<-function(data=-9){ \"Objective: find the locations of different variables for the read.fwf() function (fixed length function) data : a few records Example > # SEC index file infile<-'http://datayyy.com/data_txt/index2024Q1_100linex.txt' x<-readLines(infile) head(x,20) b<-x[9:12] > .ruler(b) [1] ----5---10---15---20---25---30---35---40---45---50---55---60---65---70---75---80---85---90---95--100---05---10---15---20---25---30---35---40---45---50 [2] Company Name Form Type CIK Date Filed File Name [3] --------------------------------------------------------------------------------------------------------------------------------------------- [4] ----5---10---15---20---25---30---35---40---45---50---55---60---65---70---75---80---85---90---95--100---05---10---15---20---25---30---35---40---45---50 \";.zruler(data)} .zruler<-function(data){ if(length(data)>1){ a<-\"----5---10---15---20---25---30---35---40---45---50\" b<-\"---55---60---65---70---75---80---85---90---95--100\" d<-\"---05---10---15---20---25---30---35---40---45---50\" f<-paste(a,b,d,sep='') final<-c(f,data,f) print(final,quote=F) }else{ if(data==-9){ print(.ruler) } } } # source('http://datayyy.com/fmr/starter/ruler.R.txt') /////////////////////// " .C8EXPLAIN23<-" /////////////////////// source('http://datayyy.com/fmr/starter/ruler.R.txt') path<-\"http://datayyy.com/data_txt/\" file<-\"index2024Q1_100lines.txt\" infile<-paste0(path,file) x<-readLines(infile) b<-x[9:12] .ruler(b) /////////////////////// " .C8EXPLAIN24<-"Top part of an index file (2024 Q1) /////////////////////// Description: Master Index of EDGAR Dissemination Feed by Company Name Last Data Received: March 31, 2024 Comments: webmaster@sec.gov Anonymous FTP: ftp://ftp.sec.gov/edgar/ Company Name Form Type CIK Date Filed File Name --------------------------------------------------------------------------------------------------------------------------------------------- &Open Gifts Ltd D 2010293 2024-02-02 edgar/data/2010293/0000950138-24-000003.txt &PARTNERS 13F-HR 107136 2024-01-09 edgar/data/107136/0001214659-24-000442.txt &PARTNERS MA-A 107136 2024-02-13 edgar/data/107136/0000107136-24-000001.txt 011235813 Inc. D 2009806 2024-02-01 edgar/data/2009806/0002009806-24-000002.txt 016 - Series of IPOSharks Venture Master Fund, LLC D 2001316 2024-01-12 edgar/data/2001316/0002000505-24-000002.txt 016 - Series of IPOSharks Venture Master Fund, LLC D/A 2001316 2024-03-18 edgar/data/2001316/0002001316-24-000001.txt 018 - Series of IPOSharks Venture Master Fund, LLC D 2000505 2024-01-12 edgar/data/2000505/0002000505-24-000001.txt 020 - Series of IPOSharks Venture Master Fund, LLC D 2002422 2024-03-18 edgar/data/2002422/0002001316-24-000002.txt 023 - Series of IPOSharks Venture Master Fund, LLC D 2004816 2024-02-07 edgar/data/2004816/0002004816-24-000001.txt 1 800 FLOWERS COM INC 10-Q 1084869 2024-02-08 edgar/data/1084869/0001437749-24-003476.txt 1 800 FLOWERS COM INC 4 1084869 2024-02-12 edgar/data/1084869/0001437749-24-003683.txt 1 800 FLOWERS COM INC 8-K 1084869 2024-01-02 edgar/data/1084869/0001437749-24-000106.txt 1 800 FLOWERS COM INC 8-K 1084869 2024-02-01 edgar/data/1084869/0001437749-24-002750.txt 1 800 FLOWERS COM INC CORRESP 1084869 2024-03-21 edgar/data/1084869/0001437749-24-008788.txt 1 800 FLOWERS COM INC SC 13G 1084869 2024-02-09 edgar/data/1084869/0000354204-24-003025.txt 1 800 FLOWERS COM INC SC 13G 1084869 2024-02-14 edgar/data/1084869/0000929638-24-000715.txt 1 800 FLOWERS COM INC SC 13G/A 1084869 2024-01-24 edgar/data/1084869/0001086364-24-002364.txt 1 800 FLOWERS COM INC SC 13G/A 1084869 2024-02-14 edgar/data/1084869/0001398344-24-002990.txt 1 800 FLOWERS COM INC UPLOAD 1084869 2024-03-11 edgar/data/1084869/0000000000-24-002652.txt 1 800 FLOWERS COM INC UPLOAD 1084869 2024-03-21 edgar/data/1084869/0000000000-24-003107.txt 1 MAIN CAPITAL MANAGEMENT, LLC SC 13G/A 1992801 2024-02-14 edgar/data/1992801/0001104659-24-023141.txt 1/0 Mortgage Investment, LLC 4 1991075 2024-03-26 edgar/data/1991075/0000950172-24-000099.txt 10 Federal Self Storage Acquisition Co 4, LLC D/A 1959226 2024-01-16 edgar/data/1959226/0001578563-24-000016.txt 100 Prince Property Owner, LLC D 2006888 2024-01-16 edgar/data/2006888/0002006888-24-000001.txt 100 RELLA FUNDING L.P. D 2010621 2024-02-16 edgar/data/2010621/0001013594-24-000214.txt 100 Saint George Investors LLC D 2013037 2024-02-28 edgar/data/2013037/0002013037-24-000001.txt 10000 DAYS CAPITAL MANAGEMENT LLC SC 13D/A 1760739 2024-01-19 edgar/data/1760739/0000919574-24-000471.txt 10000 DAYS CAPITAL MANAGEMENT LLC SC 13D/A 1760739 2024-03-07 edgar/data/1760739/0000919574-24-001920.txt 101 Avondale Street, LP D 2007220 2024-01-17 edgar/data/2007220/0002007220-24-000001.txt 101 Poe Portfolio LLC D 2016887 2024-03-27 edgar/data/2016887/0002016887-24-000001.txt 1010 E 7th LP D 1814497 2024-01-31 edgar/data/1814497/0001814497-24-000002.txt 1031CF Portfolio 4 DST D/A 1963271 2024-03-11 edgar/data/1963271/0001963271-24-000001.txt 10401 Brockington Road, LLC D 2006869 2024-01-10 edgar/data/2006869/0002006869-24-000001.txt 105 Market Street Car Wash LLC D 2013418 2024-03-25 edgar/data/2013418/0002013418-24-000001.txt 1060 Capital, LLC 13F-HR 1602119 2024-02-14 edgar/data/1602119/0001602119-24-000001.txt 1060 Capital, LLC SCHEDULE 1 1602119 2024-02-14 edgar/data/1602119/0001602119-24-000002.txt 1060 Capital, LLC SCHEDULE 1 1602119 2024-02-21 edgar/data/1602119/0001602119-24-000003.txt /////////////////////// " .C8EXPLAIN25<-" /////////////////////// path<-\"http://datayyy.com/data_txt/\" file<-\"index2024Q1_100lines.txt\" infile<-paste0(path,file) ## myWidths<-c(62, 9, 10, 15, 45) x<-read.fwf(infile,myWidths,skip=11) colnames(x)<-c(\"NAME\",\"FORM\",\"CIK\",\"DATEFILED\",\"LOCATION\") /////////////////////// " .C8EXPLAIN26<-"merge() function /////////////////////// merge(x, y, by = intersect(names(x), names(y)), by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all, sort = TRUE, suffixes = c(\".x\",\".y\"), no.dups = TRUE, incomparables = NULL, ...) /////////////////////// " .C8EXPLAIN27<-"by a common name /////////////////////// set.seed(123) n<-10 a<-data.frame(letters[1:10],rnorm(n)) colnames(a)<-c(\"ID\",\"data1\") b<-data.frame(letters[6:15],rnorm(n)) colnames(b)<-c(\"ID\",\"data2\") > z<-merge(a,b) > z ID data1 data2 1 f 1.7150650 1.2240818 2 g 0.4609162 0.3598138 3 h -1.2650612 0.4007715 4 i -0.6868529 0.1106827 5 j -0.4456620 -0.5558411 /////////////////////// " .C8EXPLAIN28<-"Merging two data sets by different names /////////////////////// Assume that we have two data sets called a, b. We intend to merge that by ID for a and NAME for b. x<-merge(a,b,by.x='ID',by.y='NAME') /////////////////////// " .C8EXPLAIN29<-"Merging 3 data sets /////////////////////// Assume that we have a, b and d x<-merge(a,b) y<-merge(d,x) /////////////////////// " .C8EXPLAIN30<-"inner-,left-,right-,all-join /////////////////////// Assume that we have two data sets A and B. inner-join: if an item exists in both A and B. left-join: if an item exists in A. right-join: if an item exists in B. out-join: if an item exists in eight A or B. Note: out-join is also called all-join /////////////////////// " .C8EXPLAIN31<-"Merging two data sets: library(dplyr) /////////////////////// library(dplyr) x<-1:10 y<-letters[1:10] data1<-data.frame(y,x) colnames(data1)<-c(\"ID\",\"item1\") set.seed(123) x<-rnorm(6) y<-letters[2:7] data2<-data.frame(y,x) colnames(data2)<-c(\"ID\",\"item2\") leftJoin <-data1 %>% left_join(data2,by='ID') rightJoin <-data1 %>% right_join(data2,by='ID') innerJoin <-data1 %>% inner_join(data2,by='ID') fullJoin <-data1 %>% full_join(data2,by='ID') # right_join format: new<-dataset1 %>% right_join(dataset2,by=c('column1','column2') /////////////////////// " .C8EXPLAIN32<-" /////////////////////// A tibble is a modern version of a data frame in R, provided by the `tibble` package, which is part of the tidyverse collection of packages. Tibbles aim to improve the data frame by: 1. Enhanced Printing: Tibbles have a concise and informative print method, showing only the first ten rows and the columns that fit on the screen, which helps in handling large datasets. 2. Strict Subsetting: Tibbles do not automatically convert strings to factors and do not change the types of variables when subsetting. 3. Better Error Messages: They provide more informative error messages, making debugging easier. 4. Column Names Handling: Tibbles support non-syntactic names and do not force the column names to be valid R variable names. These features make tibbles particularly useful for data analysis and manipulation in R, enhancing the usability and readability of the data frames. library(tibble) my_tibble <- tibble( x = 1:5, y = c(\"a\", \"b\", \"c\", \"d\", \"e\"), z = x * 2 ) print(my_tibble) /////////////////////// " .C8EXPLAIN33<-"more tibble examples /////////////////////// library(tibble) # Create a tibble my_tibble <- tibble( x = 1:5, y = c(\"a\", \"b\", \"c\", \"d\", \"e\"), z = x * 2 ) # Subsetting a tibble by column names subset_tibble <- my_tibble[, c(\"x\", \"y\")] print(subset_tibble) # ----------------------------------- library(tibble) # Create a tibble with non-syntactic names non_syntactic_tibble <- tibble( 'Column 1' = 1:3, 'Column 2' = c(\"x\", \"y\", \"z\") ) print(non_syntactic_tibble) /////////////////////// " .C8EXPLAIN34<-"factor /////////////////////// We can use the following functions: is.factor(x) is.ordered(x) as.factor(x) as.ordered(x) https://www.rdocumentation.org/packages/base/versions/3.6.1/topics/factor /////////////////////// " .C8EXPLAIN35<-"Converting factors to values /////////////////////// Example #1: simple one > f<-factor(c(1,2,5,6,5)) > f[2] [1] 2 Levels: 1 2 5 6 > f[2]+1 [1] NA > a<-as.numeric(as.character(f)) > a+0.5 [1] 1.5 2.5 5.5 6.5 5.5 Example #2: Fama-Fench 3-factor time series x<-read.csv('c:/temp/ff3Monthly.csv') n<-nrow(x) m<-ncol(x) names<-colnames(x) y<-data.frame(x[,1],factor(x[,2]),factor(x[,3]),factor(x[,4]),factor(x[,5])) colnames(y)<-names head(y,2) DATE MKT_RF SMB HML RF 1 1926-09-01 0.0036 -0.0132 1e-04 0.0023 2 1926-10-01 -0.0324 4e-04 0.0051 0.0032 y[1,2] [1] 0.0036 800 Levels: -0.2913 -0.2382 -0.2324 -0.2195 -0.2051 -0.2012 -0.1796 -0.1723 -0.1627 -0.1608 -0.1524 -0.1361 -0.1353 -0.1324 -0.1317 -0.129 -0.1275 -0.1274 ... 0.3885 y2<-as.matrix(y) y3<-matrix(as.numeric(y2[,2:m]),n,m) y4<-data.frame(x[,1],y3) colnames(y4)<-names > y4[1,2] [1] 0.0036 > y4[1,2]+10 [1] 10.0036 /////////////////////// " .C8EXPLAIN36<-"dplyr R package /////////////////////// library(dplyr) help(package=dplyr) /////////////////////// " .C8EXPLAIN37<-"%>% and group_by() function /////////////////////// library(dplyr) x <- data.frame( Person= c(\"Alice\", \"Bob\", \"Carol\", \"Dave\", \"Eve\", \"Frank\"), Region = c(\"North\", \"South\", \"North\", \"East\", \"South\", \"East\"), Sales = c(250, 300, 150, 200, 350, 180) ) total_sales_by_region <- x %>% group_by(Region) %>% summarize(TotalSales = sum(Sales)) # /////////////////////// " .C8EXPLAIN38<-"filter() function /////////////////////// library(dplyr) x<- data.frame( Name=c(\"Orange\",\"Apple\",\"Lemon\",\"Banana\",\"Grapefruit\"), Type = c(\"Citrus\",\"Pome\",\"Citrus\",\"Berry\",\"Citrus\"), Price = c(1.00, 1.50, 0.75, 0.50, 1.25) ) cheap_fruits <- x%>% filter(Price < 1.00) print(cheap_fruits) Name Type Price 1 Lemon Citrus 0.75 2 Banana Berry 0.50 /////////////////////// " .C8EXPLAIN39<-" select() /////////////////////// library(dplyr) x <- data.frame( Name = c(\"Orange\", \"Apple\", \"Lemon\", \"Banana\", \"Grapefruit\"), Type = c(\"Citrus\", \"Pome\", \"Citrus\", \"Berry\", \"Citrus\"), Price = c(1.00, 1.50, 0.75, 0.50, 1.25), Quantity = c(10, 20, 15, 30, 12) ) selected_columns <- x %>% select(Name, Price) print(selected_columns) /////////////////////// " .C8EXPLAIN40<-" /////////////////////// library(dplyr) x <- data.frame( Name = c(\"Orange\", \"Apple\", \"Lemon\", \"Banana\", \"Grapefruit\"), Type = c(\"Citrus\", \"Pome\", \"Citrus\", \"Berry\", \"Citrus\"), Price = c(1.00, 1.50, 0.75, 0.50, 1.25), Quantity = c(10, 20, 15, 30, 12) ) x2 <- x %>% select(starts_with(\"P\")) /////////////////////// " .C8EXPLAIN41<-" /////////////////////// https://www.youtube.com/watch?v=_PVu7Lr70MI /////////////////////// " .C8EXPLAIN42<-" /////////////////////// 8.34 Explain using the spread() function in tidyr to reshape data from long to wide format. 8.35 What are pivot_longer() and pivot_wider() functions used for in tidyr? 8.21 What are the advantages of using `dplyr` and `tidyr` for data manipulation compared to base R functions? /////////////////////// "