Here is a detailed answer
# Get 10 numbers for final_distance column using sample()
final_distance = sample(1:10)
# Get 10 numbers for radiant column using sample()
radiant = sample(11:20)
# Create a data frame using the columns final_distance and radiant
df <- data.frame(final_distance, radiant)
# Write a function
coordx<-function(x, y){
new <-(sin(y) * x)
return(new)
}
# Filter the unwanted values in the radiant column
I considered to ignore values that are multiples of 3 (You can write your own criteria)
df.filtered <- df[df$radiant%%3 != 0,]
# Add new column 'newcol' to data frame 'df' using the above defined function coordx
df$newcol <- coordx(df.filtered$final_distance, df.filtered$radiant)
# Print the df finally (along with the new column).
df
As per your recent comment, I am adding the second piece of code with radiant as a colu,m with string values.
# Get 10 numbers for final_distance column using sample()
final_distance = sample(1:10)
# Get 10 strings for radiant column using sample()
radiant = sample(11:20)
# Another column besides final_distance and radiant that has only strings
Other_column = sample(c("one", "two", "three", "four"), 10, replace = TRUE)
# Create a data frame using the columns final_distance and radiant
df <- data.frame(final_distance, radiant, other_column)
> str(df)
'data.frame': 10 obs. of 3 variables:
$ final_distance: int 4 3 8 7 5 6 10 1 9 2
$ radiant : int 13 16 12 11 19 14 15 18 20 17
$ other_column : Factor w/ 4 levels "four","one","three",..: 2 3 2 4 3 2 4 2 1 4
# Write a function
coordx<-function(x, y){
new <-(sin(y) * x)
return(new)
}
# Filter the unwanted values in the radiant column
# I considered to ignore values that are multiples of 3 (You can write your own criteria)
df.filtered <- df[!(df$other_column %in% c("one", "three")), ]
# Add new column 'newcol' to data frame 'df' using the above defined function coordx
df.filtered$newcol <- coordx(df.filtered$final_distance, df.filtered$radiant)
# Print the df finally (along with the new column).
df.filtered
Hope, It helps !!