Sei sulla pagina 1di 1

The built in dataset in R studio, airquality has 6 columns of data and n rows of

data. Create a new data frame which contains the data from the original data set
and a new row (append a new row) which contains the average of the each column's
data wherever applicable and NA for those columns where it is not applicable to
calculate the average.

Note : Do not use loops, use the user defined functions. The function should take
the airquality data frame as an argument

and return a new data frame with a new row added in the end.

myfunc=function(p)
{
a=sum(p[,1])
b=sum(p[,2])
c=sum(p[,3])
d=sum(p[,4])
e=sum(p[,5])
f=sum(p[,6])
l=nrow(p)
x=c(a,b,c,d,e,f)
z=x/l
df=data.frame(p)
df=rbind(df,z)
return(df)
}

print(myfunc(airquality))

Potrebbero piacerti anche