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
€64.93
List Price:  €92.76
You save:  €27.83
£53.99
List Price:  £77.13
You save:  £23.14
CA$97.22
List Price:  CA$138.90
You save:  CA$41.67
A$105.43
List Price:  A$150.62
You save:  A$45.19
S$92.47
List Price:  S$132.11
You save:  S$39.63
HK$544.12
List Price:  HK$777.35
You save:  HK$233.22
CHF 60.91
List Price:  CHF 87.02
You save:  CHF 26.11
NOK kr763.93
List Price:  NOK kr1,091.38
You save:  NOK kr327.44
DKK kr484.20
List Price:  DKK kr691.75
You save:  DKK kr207.54
NZ$116.54
List Price:  NZ$166.49
You save:  NZ$49.95
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳8,342.01
List Price:  ৳11,917.67
You save:  ৳3,575.65
₹5,905.60
List Price:  ₹8,436.93
You save:  ₹2,531.33
RM306.80
List Price:  RM438.30
You save:  RM131.50
₦117,690.28
List Price:  ₦168,136.18
You save:  ₦50,445.90
₨19,401.12
List Price:  ₨27,717.08
You save:  ₨8,315.95
฿2,383.57
List Price:  ฿3,405.25
You save:  ฿1,021.68
₺2,404.89
List Price:  ₺3,435.70
You save:  ₺1,030.81
B$398.48
List Price:  B$569.28
You save:  B$170.80
R1,224.40
List Price:  R1,749.21
You save:  R524.81
Лв126.98
List Price:  Лв181.41
You save:  Лв54.43
₩97,065.96
List Price:  ₩138,671.61
You save:  ₩41,605.64
₪260.93
List Price:  ₪372.77
You save:  ₪111.84
₱4,079.82
List Price:  ₱5,828.56
You save:  ₱1,748.74
¥10,651.14
List Price:  ¥15,216.57
You save:  ¥4,565.43
MX$1,389.48
List Price:  MX$1,985.06
You save:  MX$595.57
QR254.42
List Price:  QR363.48
You save:  QR109.05
P932.03
List Price:  P1,331.53
You save:  P399.50
KSh9,028.71
List Price:  KSh12,898.71
You save:  KSh3,870
E£3,450.40
List Price:  E£4,929.36
You save:  E£1,478.95
ብር8,593.22
List Price:  ብር12,276.56
You save:  ብር3,683.33
Kz63,785.40
List Price:  Kz91,125.91
You save:  Kz27,340.50
CLP$67,067.21
List Price:  CLP$95,814.41
You save:  CLP$28,747.20
CN¥501.07
List Price:  CN¥715.84
You save:  CN¥214.77
RD$4,196.59
List Price:  RD$5,995.39
You save:  RD$1,798.79
DA9,345.90
List Price:  DA13,351.85
You save:  DA4,005.95
FJ$158.38
List Price:  FJ$226.27
You save:  FJ$67.89
Q539.99
List Price:  Q771.44
You save:  Q231.45
GY$14,604.65
List Price:  GY$20,864.69
You save:  GY$6,260.03
ISK kr9,655.82
List Price:  ISK kr13,794.62
You save:  ISK kr4,138.80
DH690.36
List Price:  DH986.28
You save:  DH295.91
L1,255.31
List Price:  L1,793.39
You save:  L538.07
ден3,995.33
List Price:  ден5,707.86
You save:  ден1,712.53
MOP$560.14
List Price:  MOP$800.23
You save:  MOP$240.09
N$1,220.30
List Price:  N$1,743.36
You save:  N$523.06
C$2,568.69
List Price:  C$3,669.72
You save:  C$1,101.02
रु9,424.24
List Price:  रु13,463.77
You save:  रु4,039.53
S/263.85
List Price:  S/376.95
You save:  S/113.09
K280.16
List Price:  K400.24
You save:  K120.08
SAR262.89
List Price:  SAR375.57
You save:  SAR112.68
ZK1,891.77
List Price:  ZK2,702.65
You save:  ZK810.87
L323.06
List Price:  L461.54
You save:  L138.47
Kč1,638.32
List Price:  Kč2,340.56
You save:  Kč702.23
Ft26,373.60
List Price:  Ft37,678.19
You save:  Ft11,304.58
SEK kr750.79
List Price:  SEK kr1,072.60
You save:  SEK kr321.81
ARS$69,550.94
List Price:  ARS$99,362.75
You save:  ARS$29,811.80
Bs484.09
List Price:  Bs691.59
You save:  Bs207.49
COP$308,434.77
List Price:  COP$440,639.99
You save:  COP$132,205.21
₡35,749.40
List Price:  ₡51,072.77
You save:  ₡15,323.36
L1,761.10
List Price:  L2,515.97
You save:  L754.86
₲541,831.22
List Price:  ₲774,077.78
You save:  ₲232,246.56
$U2,924.58
List Price:  $U4,178.15
You save:  $U1,253.57
zł280.93
List Price:  zł401.35
You save:  zł120.41
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.