Вы находитесь на странице: 1из 2

Write a function which takes USArrests as an agrument.

Loop through all the rows of the


data frame (have to use loops here), butextract only the data pertaining to assault and urban
population and store them in 2 separate vectors. Calculate the percentage of the assault per
populationand store it in a vector. Add this newly generated percentage assault vector tothe
original US Arrests data frame (as a new column) and create a new dataframe. Return this
new data frame to print its contents from there function is called.

Write a function which takes USArrests as an agrument. Loop through all the rows of the
data frame (have to use loops here), butextract only the data pertaining to assault and urban
population and store themin 2 separate vectors. Calculate the percentage of the assault per
populationand store it in a vector. Add this newly generated percentage assault vector tothe
original US Arrests data frame (as a new column) and create a new dataframe. Return this
new data frame to print its contents from there function iscalled.

Let f(a)->USArrests
f(a)=c(x,y)
where as x=assault vector
myfunction=function(df){
len=nrow(df)
v1=vector()
v2=vector()
i=1
while (i=len) {
v1=append(v1,df[I,2])
v2=append(v2,df[I,3])
i=i+1
}
v3=v1/v2*100
new_df=cbind(df,v3)
return(new_df)
}
print(myfunc(USArrests)

Вам также может понравиться