Professional Documents
Culture Documents
1. Write a R program to compare two data frames to find the row(s) in first data frame that
are not present in second data frame.
df_90 = data.frame(
"item" = c("item1", "item2", "item3"),
"Jan_sale" = c(12, 14, 12),
"Feb_sale" = c(11, 12, 15),
"Mar_sale" = c(12, 14, 15)
)
df_91 = data.frame(
"item" = c("item1", "item2", "item3"),
"Jan_sale" = c(12, 14, 12),
"Feb_sale" = c(11, 12, 15),
"Mar_sale" = c(12, 15, 18)
)
print("Original Dataframes:")
print(df_90)
print(df_91)
print("Row(s) in first data frame that are not present in second
data frame:")
print(setdiff(df_90,df_91))
OP:
[1] "Original Dataframes:"
item Jan_sale Feb_sale Mar_sale
1 item1 12 11 12
2 item2 14 12 14
3 item3 12 15 15
item Jan_sale Feb_sale Mar_sale
1 item1 12 11 12
2 item2 14 12 15
3 item3 12 15 18
[1] "Row(s) in first data frame that are not present in second
data frame:"
Mar_sale
1 12
2 14
3 15
2. Write a R program to find elements come only once that are common to both given data
frames.
OP:
"Original Dataframes"
[1] "a" "b" "c" "d" "e"
[1] "d" "e" "f" "g"
[1] "Find elements come only once that are common to both
given dataframes:"
[1] "a" "b" "c" "d" "e" "f" "g"
3. Write a R program to create a data frame using two given vectors and display the
duplicated elements and unique rows of the said data frame.
a = c(10,20,10,10,40,50,20,30)
b = c(10,30,10,20,0,50,30,30)
print("Original data frame:")
ab = data.frame(a,b)
print(ab)
print("Duplicate elements of the said data frame:")
print(duplicated(ab))
print("Unique rows of the said data frame:")
print(unique(ab))
OP:
%fa 9.5 26. 7.8 17. 31. 25. 27. 27. 31.
t 5 8 4 9 4 2 2
ag 52 54 54 56 57 58 58 60 61
e
%fa 34. 42. 28. 33. 30. 34. 32. 41. 35.
t 6 5 8 4 2 1 9 2 7
6. Suppose that the data for analysis includes the attribute age. The age values for the data
tuples are (in increasing order) 13, 15, 16, 16, 19, 20, 20, 21, 22, 22, 25, 25, 25, 25, 30, 33,
33, 35, 35, 35, 35, 36, 40, 45, 46, 52, 70.
Can you find (roughly) the first quartile (Q1) and the third quartile (Q3) of the data?
>age
[1] 13 15 16 16 19 20 20 21 22 22 25 25 25 25 30 33 33 35 35 35
35 36 40 45 46 52
[27] 70
>Q1 = quantile(age,.25)
>print(Q1)
25%
20.5
>Q3 = quantile(age,.75)
>print(Q3)
75%
35