The following code shows two things:
- How to take the full 7352 x 563 data set in train.csv, reduce it to a small sample and produce a forum-friendly output with dput().
- How to use @andresrcs's code to change the column names of the data frame.
Only the second part would normally be posted.
####First part
#read in the whole data set
DF <- read.csv("~/R/Play/train.csv", check.names = FALSE)
#Choose a few row and columns
#DFsample <- DF[1:10, 1:5]
DFsample
#> tBodyAcc-mean()-X tBodyAcc-mean()-Y tBodyAcc-mean()-Z tBodyAcc-std()-X
#> 1 0.2885845 -0.020294171 -0.1329051 -0.9952786
#> 2 0.2784188 -0.016410568 -0.1235202 -0.9982453
#> 3 0.2796531 -0.019467156 -0.1134617 -0.9953796
#> 4 0.2791739 -0.026200646 -0.1232826 -0.9960915
#> 5 0.2766288 -0.016569655 -0.1153619 -0.9981386
#> 6 0.2771988 -0.010097850 -0.1051373 -0.9973350
#> 7 0.2794539 -0.019640776 -0.1100221 -0.9969210
#> 8 0.2774325 -0.030488303 -0.1253604 -0.9965593
#> 9 0.2772934 -0.021750698 -0.1207508 -0.9973285
#> 10 0.2805857 -0.009960298 -0.1060652 -0.9948034
#> tBodyAcc-std()-Y
#> 1 -0.9831106
#> 2 -0.9753002
#> 3 -0.9671870
#> 4 -0.9834027
#> 5 -0.9808173
#> 6 -0.9904868
#> 7 -0.9671859
#> 8 -0.9667284
#> 9 -0.9612453
#> 10 -0.9727584
#Use dput to make output that can be posted to the forum and used easily by others
dput(DFsample)
#> structure(list(`tBodyAcc-mean()-X` = c(0.28858451, 0.27841883,
#> 0.27965306, 0.27917394, 0.27662877, 0.27719877, 0.27945388, 0.27743247,
#> 0.27729342, 0.28058569), `tBodyAcc-mean()-Y` = c(-0.020294171,
#> -0.016410568, -0.019467156, -0.026200646, -0.016569655, -0.01009785,
#> -0.019640776, -0.030488303, -0.021750698, -0.0099602983), `tBodyAcc-mean()-Z` = c(-0.13290514,
#> -0.12352019, -0.11346169, -0.12328257, -0.11536185, -0.10513725,
#> -0.11002215, -0.12536043, -0.12075082, -0.10606516), `tBodyAcc-std()-X` = c(-0.9952786,
#> -0.99824528, -0.99537956, -0.99609149, -0.99813862, -0.99733496,
#> -0.99692104, -0.99655926, -0.99732847, -0.99480344), `tBodyAcc-std()-Y` = c(-0.98311061,
#> -0.97530022, -0.96718701, -0.9834027, -0.98081727, -0.99048681,
#> -0.96718593, -0.96672843, -0.96124532, -0.9727584)), row.names = c(NA,
#> 10L), class = "data.frame")
######Second part
#Use the output of dput to make a small data frame
DF_forum <- structure(list(`tBodyAcc-mean()-X` = c(0.28858451, 0.27841883,
0.27965306, 0.27917394, 0.27662877, 0.27719877, 0.27945388, 0.27743247,
0.27729342, 0.28058569),
`tBodyAcc-mean()-Y` = c(-0.020294171, -0.016410568, -0.019467156, -0.026200646, -0.016569655, -0.01009785,
-0.019640776, -0.030488303, -0.021750698, -0.0099602983),
`tBodyAcc-mean()-Z` = c(-0.13290514,
-0.12352019, -0.11346169, -0.12328257, -0.11536185, -0.10513725,
-0.11002215, -0.12536043, -0.12075082, -0.10606516),
`tBodyAcc-std()-X` = c(-0.9952786,
-0.99824528, -0.99537956, -0.99609149, -0.99813862, -0.99733496,
-0.99692104, -0.99655926, -0.99732847, -0.99480344),
`tBodyAcc-std()-Y` = c(-0.98311061,
-0.97530022, -0.96718701, -0.9834027, -0.98081727, -0.99048681,
-0.96718593, -0.96672843, -0.96124532, -0.9727584)),
row.names = c(NA, 10L), class = "data.frame")
DF_forum
#> tBodyAcc-mean()-X tBodyAcc-mean()-Y tBodyAcc-mean()-Z tBodyAcc-std()-X
#> 1 0.2885845 -0.020294171 -0.1329051 -0.9952786
#> 2 0.2784188 -0.016410568 -0.1235202 -0.9982453
#> 3 0.2796531 -0.019467156 -0.1134617 -0.9953796
#> 4 0.2791739 -0.026200646 -0.1232826 -0.9960915
#> 5 0.2766288 -0.016569655 -0.1153619 -0.9981386
#> 6 0.2771988 -0.010097850 -0.1051373 -0.9973350
#> 7 0.2794539 -0.019640776 -0.1100221 -0.9969210
#> 8 0.2774325 -0.030488303 -0.1253604 -0.9965593
#> 9 0.2772934 -0.021750698 -0.1207508 -0.9973285
#> 10 0.2805857 -0.009960298 -0.1060652 -0.9948034
#> tBodyAcc-std()-Y
#> 1 -0.9831106
#> 2 -0.9753002
#> 3 -0.9671870
#> 4 -0.9834027
#> 5 -0.9808173
#> 6 -0.9904868
#> 7 -0.9671859
#> 8 -0.9667284
#> 9 -0.9612453
#> 10 -0.9727584
#rename the columns
library(stringr)
names(DF_forum) <- str_remove_all(names(DF_forum), "[-()]")
DF_forum
#> tBodyAccmeanX tBodyAccmeanY tBodyAccmeanZ tBodyAccstdX tBodyAccstdY
#> 1 0.2885845 -0.020294171 -0.1329051 -0.9952786 -0.9831106
#> 2 0.2784188 -0.016410568 -0.1235202 -0.9982453 -0.9753002
#> 3 0.2796531 -0.019467156 -0.1134617 -0.9953796 -0.9671870
#> 4 0.2791739 -0.026200646 -0.1232826 -0.9960915 -0.9834027
#> 5 0.2766288 -0.016569655 -0.1153619 -0.9981386 -0.9808173
#> 6 0.2771988 -0.010097850 -0.1051373 -0.9973350 -0.9904868
#> 7 0.2794539 -0.019640776 -0.1100221 -0.9969210 -0.9671859
#> 8 0.2774325 -0.030488303 -0.1253604 -0.9965593 -0.9667284
#> 9 0.2772934 -0.021750698 -0.1207508 -0.9973285 -0.9612453
#> 10 0.2805857 -0.009960298 -0.1060652 -0.9948034 -0.9727584
Created on 2023-03-04 with reprex v2.0.2