i have a data frame with 2 columns and i want o add a third based on what is contained in my second column,
i have columns speed ( integer) , column measure ( in knots and km) I want to add a column that calculates the km per hr speed if colunm 2 is knots or returns the value in the speed colum which is already in km , i have tried the below for loop which returns with an error "number of items to replace is not a multiple of replacement lengthnumber"
my for loop is
selected_storms_2$KM_Wind_Speed <- c()
for (j in 1: length(selected_storms_2$measure))
{
if(selected_storms_2$measure[j] == 'knots' )
{selected_storms_2$KM_Wind_Speed[j] <- selected_storms_2$Speed *1.8 }
else if (selected_storms_2$measure[j] == 'km' )
{selected_storms_2$KM_Wind_Speed[j] <- selected_storms_2$Speed}
}
can anyone see what i am doing wrong ? is there another way ?
If your measure column includes more values than 'knots' and 'km', then this will not work as written, but can be solved by nesting ifelse() statements.
If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it: