Objective: To import the Fitbit daily Movement records into the R language for analysis and to draw a statistical chart to
Original data: fitbit2014 Daily Record spreadsheet file, examples are as follows:
Date |
Calories burned |
Step |
Distance |
Number of floors climbed |
A sedentary number of minutes |
Less active minutes |
Moderate active minutes |
Very active minutes |
April 27, 2014 |
2736 |
16581 |
11.84 |
7 |
1111 |
131 |
117 |
81 |
April 28, 2014 |
2514 |
12622 |
9.01 |
6 |
910 |
136 |
59 |
76 |
April 29, 2014 |
2231 |
8357 |
5.97 |
9 |
1208 |
129 |
76 |
27 |
April 30, 2014 |
2247 |
7959 |
5.68 |
9 |
1196 |
137 |
83 |
24 |
May 1, 2014 |
2563 |
13014 |
9.29 |
21st |
1107 |
156 |
131 |
46 |
May 2, 2014 |
2376 |
10010 |
7.15 |
17 |
1147 |
164 |
99 |
30 |
May 3, 2014 |
2553 |
13002 |
9.28 |
25 |
1119 |
151 |
120 |
50 |
May 4, 2014 |
2370 |
10090 |
7.23 |
19 |
1173 |
147 |
82 |
38 |
1) Convert XLS to CSV
First of all to import the XLS spreadsheet, here in the simplest way, first save the XLS as a CSV file. In order to enter the command conveniently, the column name is changed to English.
2) Copy the CSV to the working directory, or set up your own working directory so that the R environment can find the appropriate files
Put the CSV in the working directory, or you can set your working directory with SETWD ("C:/myfitbit")
3) Import CSV file to data frame
Fitbit <-read.table ("Fitbit.csv", Header=true, sep= ",", row.names= "date")
You can also call the Read.csv () function directly, and you can fill in a few parameter options
4) Look at the overview of the data
Summary (Fitbit)
You can see statistics such as the minimum, maximum, average, median, and so on for each column of data.
Ka Step dist Floor
Min.: 2031 min.: 0 min.: 0.000 min.: 0.00
1st qu.:2290 1st Qu.: 8593 1st Qu.: 6.135 1st Qu.: 11.00
median:2408 median:10515 median:7.570 median:16.00
mean:2432 mean:10152 mean:7.274 mean:17.35
3rd qu.:2547 3rd qu.:12665 3rd Qu.: 9.120 3rd Qu.: 20.00
Max. : 3360 Max. : 25779 Max. : 18.410 Max. : 165.00
Sit inactive Move Active
Min.: 829 min.: 0.0 min.: 0.00 min.: 0.00
1st qu.:1119 1st qu.:126.0 1st Qu.: 75.00 1st Qu.: 18.50
median:1159 median:146.0 median:93.00 median:37.00
mean:1170 mean:137.3 mean:91.89 mean:37.26
3rd qu.:1188 3rd qu.:163.0 3rd qu.:113.00 3rd Qu.: 51.00
Max. : 1440 Max. : 238.0 Max. : 222.00 Max. : 137.00
5) Look at the structure of the data frame, which is the type of each column?
With the STR function,str is the abbreviation for structure words.
STR (Fitbit)
' Data.frame ': 243 obs. of 8 variables:
$ ka:num 2496 2513 2228 2336 2508 ...
$ step:num 12803 12955 8768 8971 12078 ...
$ dist:num 9.14 9.47 6.31 6.41 9.05 ...
$ floor:int 15 12 16 16 8 20 12 13 13 13 ...
$ sit:num 1161 1188 1234 1140 1153 ...
$ inactive:int 123 112 97 174 130 177 156 121 126 123 ...
$ move:int 98 67 72 113 108 141 99 118 65 73 ...
$ active:int 58 73 37 13 49 61 40 37 47 56 ...
6) Draw a chart of the number of steps per day
Plot (Fitbit$step)
To remove a column, you can use the $ symbol.
If it's too cumbersome to write fitbit$ each time, you can use Attach (Fitbit) to add the search path to the data frame so that you can enter step to represent Fitbit$step later.
In contrast to attach () is the detach () function, in order to avoid programming errors, it is best to appear in pairs.
7) What is the relationship between the number of steps to walk and the number of kilometres?
Plot (SETP, Dist)
You can see that Fitbit is actually a very simple algorithm to convert the number of steps to kilometers, and no advanced calculations.
8) Add title to the graph
Title ("Fitbit Chart")
9) Output the graphic to a JPEG file in the form of a command
JPEG ("a.jpg") #将绘图送到jpeg图形文件中
Plot (step, Dist)
Title ("Fitbit Chart")
Dev.off () #最后要关闭设备
Other commands: Dev.new () can open a new drawing window. Dev.next (), Dev.prev (), Dev.set () ...
Of course, right-click on the graph in the R interactive environment, or use the menu function of the R environment, you can copy and output the graphic randomly.
10) Look at the statistical map of the steps
hist (STEP)
With an average of more than 10,000 steps per day, some records were lost during Fitbit Sync, so more than 20 days went 0 steps.
11) Display Date
The date is recorded in a string format such as "October 28, 2014", which is not converted to date type and needs to be used as. The date function is converted.
Month <-as. Date (Row.names (Fitbit), "%y year%m month%d Day")
Plot (month, step)
You can see that the horizontal axis is already showing the date.
Analyze my Fitbit pedometer data in R language