In: Computer Science
Using R language: Given a vector or list of numbers, call this variable x , write down the code that will create a new vector, y , where all the odd number entries are set to 0. In other words, the first, third, fifth, etc entries should all be 0.
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly
indented for better understanding.
# Initialise a list
list_data <- list(1,2,3,4,5,6,7)
#Create a new list
filtered_data <- list()
i<-0
while(i<=length(list_data))
{
#Even index? copy data at ith index
if(i%%2 == 0)
{
filtered_data[i]=list_data[i];
}
else
{
#Fill ZERO
filtered_data[i]=0;
}
#Increase i value
i<- i+1;
}
#Print the data filtered
print(filtered_data)
=============
SCREENSHOT: