Week 1
Week 1
R Programming
8. In the deleting of row or column in a dataframe df[-4,] is just removing the data
from the view actually it is not getting deleted.
Answer:
The command df[-4,] deletes the 4th row of the dataframe, if there exist 4th row.
10. While using print(list$listelement) to access the elements of a list, the console
shows NULL instead of showing the list element.
Answer:
NULL represents the null object in R: it is a reserved word.
NULL is often returned by expressions and functions whose values are
undefined. Please check if you have defined the elements of the list properly.
11. In vectors, it is said that elements of a vector must be of the same data type but
the following code doesn't give any error > x=c(2.3,4.5,"vinu").
Answer:
The elements of a vector must be of same data type. Since you have given vinu
within quotes, by default R considers all other elements in that variable are of
character data type. That is why it does not throw any errors.
12. Is it correct to say that the "source(ctrl+shift+S)" as compilation of program and
not displaying and that "Source with echo (ctrl+Shift+Enter)" as execution of
program?
Answer:
Source will execute all commands in console without displaying them whereas
source with echo will execute and print automatically.
14. How to join data frames pd, pd_new if not having common column names?
How to join the 2 columns Name and Name1?
Answer:
full_join() returns all rows and all columns from both x and y. Where there are not
matching values, returns NA for the one missing. You can try this out.
16. When running RStudio on a mac, a pop up showing unable to locate R binary by
scanning standard locations appears.
Answer:
Try executing the following commands in a terminal:
$ echo $PATH
$ which R
$ ls -la /usr/bin/R
$ ls -la /usr/local/bin/R
$ ls -la /opt/local/bin/R
Finally, if you launch RStudio from the command line
$ open -a RStudio
17. Whenever executing the following code print(object) it shows object not found.
Answer:
The object is nothing but any objects in R such as matrices and data frames. The
error might come when you have not run that particular object before printing it.
18. We can edit a particular string element using slicing operator (df[[2]][2] = “R”)
, are there other ways?
Answer:
There are several ways to edit the data frame, some of them have explained in
the videos. (df[[2]][2] = “R”) explains editing an element alone. $ operator can
also be used to edit directly. Every editing commands does the same.
19. While editing, the values are not saved after closing the particular dialogue box.
Answer:
The edited values need to be saved to a variable, otherwise it will not get saved.
21. What are the scenarios where one can use Recasting?.
Answer:
Ultimately every row corresponds to the observation and every column to
variable. The observations should be unique. There should not be any
repetitions. By using Recasting method we can have a meaningful data frame.
22. While having option to save entire R program code and why one should save
individual data?
Answer:
Let us suppose that you have done some imputation on your data and when
coming back after saving that you do not have to run all the codes again, it is
enough to load the data into R, and can start from where you have left.
24. X = c(2.3,4.5,6.7,8.9) print (X) output is [1] 2.3,4.5,6.7,8.9 what is [1] here and
how does it come?.
Answer:
[1] represents the layer. Since we have only one vector /row it shows [1].
25. When running the above code it shows error plot(x,y,type=l) Error in plot.xy(xy,
type, ...) : object 'l' not found.
Answer:
In plot(x,y,type=l) , l should be with in the quotes ('l')
27. While trying to concatenate lists, what will happen if one doesn't have the same
number of elements? ie., in my employee list I have 5 elements and the newly
added ages lists, that is being concatenated has 4 elements.
Answer:
It just concatenates irrespective of the size of the lists.
31. How to create a matrix with different values for eg.negative values?
Answer:
a<-matrix(c(1,-1,2),nrow=1,ncol=3)
32. In the command apply(A,1 or 2,sum), 1 indicates row and 2 indicates column.
Does that remain constant for all situations?
Answer:
Yes it remains constant.
When running the code, it's replacing the value "Kunal" by "N/A":
➢ Info[[2]][1] = "Kunal Gaurav"
Answer:
while creating a dataframe, set stringsAsFactors = F.
SerialNum = c(1, 2, 3)
Name = c("Kunal", "Indranil", "Harsh")
EmployedStatus = c("Accenture", "Capgemini", "No")
#Data Frame creation syntax
Info = data.frame(SerialNum, Name, EmployedStatus,stringsAsFactors = F)
print(Info) #Assigning new value to a data frame using direct assignment
Info[[2]][2] = "Kunal Gaurav"
Info
https://www.rdocumentation.org/packages/base/versions/3.5.2/topics/c
37. rm(list=ls(b,end_point))
shows below error,
please guide
Error in as.environment(pos) : invalid 'pos' argument.
Answer:
To clear a specific variable : rm(variablename)
To clear all the variables :rm(list=ls())
38. How to increase the number of rows and column in edit table?
Answer:
It is like a spreadsheet, you can add the values to the rows and columns.
39. While trying to execute the left join got an error message:
Warning message: Column `Name` joining factors with different levels, coercing
to character vector.
Observed that the dataframe1 (pd) and dataframe2(pd_new) has different factor
levels. How to get rid of this error?
Answer:
In R, You cannot add/ delete existing factors / categories in a data frame. If you
want to do so, you will have to convert factors to character data type before doing
that. Similarly, while joining data frames it automatically converts factor data type
to character data type since Name has different levels. Please find below the
snippet for your reference.
40. What is the difference between 'print(a) 'or ‘print(c(a))' where a is any vector or
list.
Answer:
print()- it prints the argument
print(c(a)) - it converts the data frame to a vector or list
id=c(29,30,31,32)
➢ names=c("bruh","blah","bleh","bloh")
➢ score=c(95,85,84,87)
➢ gender=c("f","m","f","m")
➢ df=data.frame(id,names,score,gender)
➢ print(df)
id names score gender
1 29 bruh 95 f
2 30 blah 85 m
3 31 bleh 84 f
4 32 bloh 87 m
➢ df1=df[4,3]
➢ print(df1)
[1] 87
Expecting the output of df[4,3] as the value of 4th row and 3rd column , which is,
"f".
But the output turned out to be the value of 3rd row and 4th column, i.e. 87.
42. Is that necessary to set the working directory every time to write a program or
code?.
Answer:
While reading the data, it is necessary to set the working directory.
44. Is it necessary to use " " for numbers? If " " is used will it automatically takes it
as a character?
Answer:
For numerical variable double quotes is not required. If double quotes are added
in the numerical variable, R automatically converts to character.
45. Why does as.numeric("a") return NA? Why doesn't it return its ASCII value?.
Answer:
R does not return ASCII value.
Coercions from character to numeric variable is not possible so it returns NA
50. Why is dataframe important for storing data? Why can't we store using
hashtable?
Answer:
You can also store the data using hashtable.
52. While installing the packages it says, package ‘plyr’ is not available (for R
version 3.5.2) 'lib = "dplyr"' is not writable.
Answer:
Kindly install dplyr.
install.packages("dplyr")
56. The argument taken for margin for adding rows and columns are '1' and '2'.Is
there any explanation for this ?
Answer:
To calculate row sum please specify margin=1 and for column sum, margin= 2.
Apart from calculating sum we can also find mean and other operations.
57. When trying manipulation using stringsAsFactors it was not removing the invalid
factor message.
Answer:
Kindly set the stringAsFactors = F.
60. The mutate command is adds a column in a Dataframe. What is the difference
between cbind and mutate command.
Answer:
cbind is used to combine the columns in the dataframe.
mutate() adds new variables and preserves existing ones.
61. Can we add any row without entry of a specific column using rbind command ? If
yes then what is the syntax?.
Answer:
You can add a row without entry for a specific column using "" . It will be
considered as NA.
Please find attached snapshot for your reference.
62. Why do we use c before () in any vector or data frame?
Answer:
c() - combines the values to a vector or a list.
63. install.packages("reshape2")
trying URL 'https://cloud.r-project.org/bin/windows/contrib/3.4/reshape2_1.4.3.zip'
Content type 'application/zip' length 611622 bytes (597 KB)
downloaded 597 KB
package ‘reshape2’ successfully unpacked and MD5 sums checked
➢ library(“reshape2”)
Error: unexpected input in "library(“"
Answer:
To load the library please use the following command:
library(reshape2)