Dealing with Files containing Data using R

Learning R through an Example Learning R through an Example - Part 1
16 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.18
List Price:  €95.97
You save:  €28.79
£55.85
List Price:  £79.79
You save:  £23.94
CA$98.21
List Price:  CA$140.31
You save:  CA$42.09
A$107.62
List Price:  A$153.75
You save:  A$46.13
S$94.24
List Price:  S$134.64
You save:  S$40.39
HK$544.83
List Price:  HK$778.36
You save:  HK$233.53
CHF 62.54
List Price:  CHF 89.34
You save:  CHF 26.80
NOK kr774.97
List Price:  NOK kr1,107.14
You save:  NOK kr332.17
DKK kr501
List Price:  DKK kr715.75
You save:  DKK kr214.74
NZ$119.95
List Price:  NZ$171.37
You save:  NZ$51.41
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,361.58
List Price:  ৳11,945.63
You save:  ৳3,584.04
₹5,909.84
List Price:  ₹8,442.99
You save:  ₹2,533.15
RM312.71
List Price:  RM446.75
You save:  RM134.04
₦118,484.88
List Price:  ₦169,271.38
You save:  ₦50,786.49
₨19,443.22
List Price:  ₨27,777.22
You save:  ₨8,334
฿2,412.28
List Price:  ฿3,446.26
You save:  ฿1,033.98
₺2,418.32
List Price:  ₺3,454.90
You save:  ₺1,036.57
B$406.01
List Price:  B$580.04
You save:  B$174.03
R1,270.71
List Price:  R1,815.38
You save:  R544.66
Лв131.35
List Price:  Лв187.66
You save:  Лв56.30
₩98,301.65
List Price:  ₩140,436.95
You save:  ₩42,135.30
₪260.01
List Price:  ₪371.46
You save:  ₪111.45
₱4,125.14
List Price:  ₱5,893.31
You save:  ₱1,768.17
¥10,832.35
List Price:  ¥15,475.45
You save:  ¥4,643.10
MX$1,429.79
List Price:  MX$2,042.64
You save:  MX$612.85
QR255.18
List Price:  QR364.56
You save:  QR109.38
P956.90
List Price:  P1,367.06
You save:  P410.16
KSh9,061.51
List Price:  KSh12,945.58
You save:  KSh3,884.06
E£3,475.01
List Price:  E£4,964.52
You save:  E£1,489.50
ብር8,566
List Price:  ብር12,237.67
You save:  ብር3,671.66
Kz63,900.87
List Price:  Kz91,290.87
You save:  Kz27,390
CLP$68,444.99
List Price:  CLP$97,782.75
You save:  CLP$29,337.76
CN¥506.93
List Price:  CN¥724.22
You save:  CN¥217.29
RD$4,217.06
List Price:  RD$6,024.63
You save:  RD$1,807.57
DA9,397.90
List Price:  DA13,426.15
You save:  DA4,028.25
FJ$159.29
List Price:  FJ$227.57
You save:  FJ$68.27
Q540.13
List Price:  Q771.64
You save:  Q231.51
GY$14,638.82
List Price:  GY$20,913.50
You save:  GY$6,274.67
ISK kr9,773.54
List Price:  ISK kr13,962.80
You save:  ISK kr4,189.25
DH703.91
List Price:  DH1,005.63
You save:  DH301.72
L1,275.33
List Price:  L1,821.98
You save:  L546.64
ден4,132.76
List Price:  ден5,904.20
You save:  ден1,771.43
MOP$561.01
List Price:  MOP$801.48
You save:  MOP$240.46
N$1,268.91
List Price:  N$1,812.81
You save:  N$543.90
C$2,574.70
List Price:  C$3,678.31
You save:  C$1,103.60
रु9,449.77
List Price:  रु13,500.25
You save:  रु4,050.48
S/265.32
List Price:  S/379.05
You save:  S/113.72
K281.71
List Price:  K402.47
You save:  K120.75
SAR262.77
List Price:  SAR375.40
You save:  SAR112.63
ZK1,934.92
List Price:  ZK2,764.29
You save:  ZK829.37
L334.42
List Price:  L477.77
You save:  L143.34
Kč1,702.59
List Price:  Kč2,432.37
You save:  Kč729.78
Ft27,646.05
List Price:  Ft39,496.05
You save:  Ft11,850
SEK kr772.42
List Price:  SEK kr1,103.50
You save:  SEK kr331.08
ARS$70,251.41
List Price:  ARS$100,363.46
You save:  ARS$30,112.05
Bs483.50
List Price:  Bs690.75
You save:  Bs207.24
COP$307,238.59
List Price:  COP$438,931.09
You save:  COP$131,692.49
₡35,641.51
List Price:  ₡50,918.63
You save:  ₡15,277.11
L1,768.23
List Price:  L2,526.16
You save:  L757.92
₲546,248.87
List Price:  ₲780,388.98
You save:  ₲234,140.10
$U2,983.14
List Price:  $U4,261.82
You save:  $U1,278.67
zł291.40
List Price:  zł416.31
You save:  zł124.90
Already have an account? Log In

Transcript

The data we will utilize in this program is the prices of commodities I have been tracking for a very long time now. Now, I use this data as I have played copyright on this and I can use it in this program without having to deal with any issues of validity and ownership of this data. The data should be sufficient to discuss the different aspects of art that we have planned in the for the program, you can get, you can try all the different concepts we will discuss using this data and you can get hold of your own data and apply these concepts to them, which you can get it from yourself or from your organization. First let us look at the data. So, I opened the file for you. The data you can see is a CSV file, comma separated values file.

There are these attributes observation date for tattoo on Ian price tomato price go In pries, Sensex index and the Nifty Index The first thing we have to do is to read the data into our our environment. Let us see how to do it. Now, to read the data we have to give the command read dot CSV. So, first thing we have to do here is to give the file name. So, within quotes give the complete path followed by the file name. So, that is rise index as of 23rd of April zero falls two three dot CSV.

Now, this data has got a header So, we say header is equal to true and this data is separated by comma So, he says separator is equal to comma okay This data has to be stored into a variable, the variable we store it is a data frame. So for this purpose, we said df is a variable and store this data in that variable. It does run this command. When we run this command, it executes successfully, so we don't have to worry. Then I see what the data looks like. So we said df and run this.

So now you see there's a lot of the all the information is shown there's a lot of information. There. Let's see the top few rows and the last few years to see the top few rows with a head here. We run this now you can see the first six rows of this data. Similarly to see the last few we can say tail, pf. And we can run this shows the last few data points in this data set.

Okay now we need to know the structure of this data to see the structure of the data we say str df. When you run this it shows the structure of the data as you can see, observation is a factor which is strings right now though it's dates it's in string format right now, but I know prize only and prize, tomato price, gold price, Sensex and Nifty Index these are all numbers. Okay, now let us see how we can see individual rows and columns of data. First, we see a number of rows, we can say n rows df, it shows the number of rows that we have, so we have 1939 rows. Similarly, we can see the number of columns, we can say n call, df, and it shows the number of columns of data that we have. Now if so we have seven columns of data.

Suppose we want to only see a few columns of data we can say df followed by bracket the first parameter is a row number. next parameter is the column number. So we say comma one. So, we will see the first column we run it and so we see all the dates on the the date column is displayed here. Okay. We can also see multiple columns at the same point of time, we can say df followed by now the first parameter is the row number then we give a vector within the vector we say one comma four comma five comma six.

So we are going to see four columns of data right now. So we say this so we get four volumes of data. We as this is difficult we can say head for this and we can only see the first few columns. first few rows. So there you are using the observation data tomato gold and Sensex, we can see particular rows also we can say, we show me the number 10 2031 and 45 and 65. So we can see individual particular roles which are doing, analyze.

So this way we can get exactly the number of the row numbers that we want to see, don't forget to put the comma up to the row number otherwise it will us in taxes. Now, there is another way to see a particular column, we can say df dollar, dollar, and then give the field name. So, I say nifty. And now let's run this. So, you see all the values of the column nifty is displayed. This is also very useful in This can be used in many other contexts as well which you will see very shortly.

Now, we can find data for a particular condition as well. So, let us say we want to find the data or date on which nifty was at the maximum. So, we can say df dollar recently nifty and then we give the condition. So, condition is which we say which and here we said df dollar nifty is equal to w equal to is the equation equator operator equal to max off df dollar nifty df dollar nifty okay. So, now let us run this okay so it says the nifty was maximum value was the 111 Three 0.4 but we want to see the date right. So, we said df.ob is date and then give the condition.

Now, when we run this, we see it was on the maximum on 29th of January 2018. Now, what if you want to see all the columns for that particular date, then we can use a subset command, we can subset. Now the data frame which we're using is df subset df and then we give the condition we just copy and paste the condition that we have given before. So df.or nifty is equal to maximum TF dot o nifty. Now let us run this. So when we run this, we get this out PC the entire tupple where this data is found.

Now, a little look at the data once again, you say head dear If we run this you'll notice that the price of potato onion and tomato is the same in all the all this data what you're seeing this is because I started recording this data only from first of January 2018. Now, the remaining data I have updated with the mean value what I have found for the observation dates. So, now, let us see on me on the one the data from first of January So, we can say df subset df df dollar OBS date greater than or equal to 2018, hyphens 01, hyphens 01 first January 2018. Now, when we run this, let us see what happens it gives an error that is it is not able to convert is not able to form this equation it says because the data is stored as a string, it is not a date.

So, let us now convert this column in into a date column do this we need to include the library I pasted the code for you already we have to use the package file Do you print it? So, that does include the package load the package called up to date you run this code now the package Liberty data is included. So now we say df dollar copious date we will try to update this columns data type df dot o p as date assign it. Now we say as date and to convert it to date we need the function as dot date. So as the date we need to supply that we want df dollar OBS date to be converted. And the existing data if you notice it's in the format why by year, month and date.

So we say percentage capital why this you can see in the syntax book function hyphen percentage m rightful percentage d. So, now let us run this. So, it does not give any errors data see whether the data actually got converted or not we say head df. Now you see in our data the data is available, it will see the structure to see whether the data type actually got changed or not. So, we say structure df now you'll notice that it is up to date format. Okay, now we can run our subset command once again. So, now we see subset df OBS date is greater than or equal to we convert that string also into a date as date of this 2001 year, followed by percentage.

Capital Y hyphen, percentage m hyphen, percentage D need to be one more bracket Okay. Now let us run this. So, when we run this, let us see the data there is a lot of data. So, you notice that these are all in 2018 okay now just see head and then see the initial data that will give a complete proof of this. So, we say head of this and we run this. Now, you see this from first of January 2018.

So, we have converted this column from a string type to our data now, we can perform data operations on this particular column. We will see those very shortly. Now, we can update data in particular columns of a data frame. Let us see how to do that, like I told you I have recorded data, protect audio And tomato prices from first of January 2018. However, we have this data in the data frame from first of January 2013. So, let us update it we said first bf potato and we do not want to update all the data.

So, we only want to update the data from before first of January 2018. So, we say which and we copy this condition paste it here, okay and we make it less than So, all that for all the rows where the date is less than first of January 2018, we want to update the data and we set the value to Na Na stands for not available. So we now we can run this command Okay, now this has gone through. Now let us very check this. So let us see the data for the potato prices. So we said Do we give a head so that you don't see a lot of data?

So, we say head first 20 rows in on it you see all the data has been changed to any we can do the same thing for the tomato and only enterprises also. So we copy this and we change this to tomato and we again copy this and change and change this date to on yen Okay, we can run these two commands. Now it is done. So we can say show me the first 20 rows and it dissolves in we can also add a new column to the data frame. Let us say we want to add the month for That observation date. So, to do that, we we can we will say df and we want to add a new column called OBS month.

So, we say df dot OBS month and we assign this to the month this month function is available in the loop to date package, month of df dollar OBS date observation date. So, now when we run this it has gone through now let us check the structure of our data. So we say str df. Now you see we have an extra column called OBS month and it is a numeric column where the month is stored. Let us see the first 20 rows and then we can see that there is a column called OBS month and it has got the month of the date. We can say Taylor of this, and we can check the last few rows as well.

And we notice that the month number is stored there. We summary summarize what we have seen so far, we saw how to read data from a CSV file. We saw how to view data view particular columns of data, one or more of them. We also saw how to view data for particular rows, one or more of them. And then we saw how to view data for a given condition. We also learned how to subset data.

We introduced you to the lubra date package we saw we told you how to handle dates. We saw how to convert the data type of a column from a string to a date. Then we saw how to update data in particular columns. And also we saw how to add a new column to the data frame. We will conclude the part one of this program here. Please join us for the part To where we'll cover the remaining topics that is dealing with databases, creating graphs and creating a presentation.

Please join us for that session. We thank you for watching our video. I hope you enjoyed it as much we hope we enjoyed creating it for you. Please join us for part two where we continue this program and look out for other programs that we will be launching in the future. Thank you once again and goodbye

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.