Reputation: 421
I have a 8 column data frame. I want to combine 2 columns of a data frame into one (column 6 and 8.) Both of these columns contain the same data. I basically want to remove the "NAs" such that there are 3 rows of "South West" and last row of "South East"
6th column 8th Column
NA ----------- South West
NA ----------- South West
NA ----------- South West
South East -- NA
I subset the data frame to only pull columns 6 and 8 (the columns that I would like to combine.)
region_merge = df[,c(6,8)]
I created my own function as below:
comb_region <- function(df){
if (df[,6]=="NA")
df[,6] = df[,8]
else
df[,6] = df[,6]
}
I then used the apply function to repeat the function across all my rows:
apply(X = region_merge, MARGIN = 1, FUN = comb_region)
However, I get the error: Error in df[, 6] : incorrect number of dimensions
I'm new to R. Could someone help me understand why I am getting this error?
Alternatively - Do you guys know an alternative solution to merge 2 columns in 1, by removing the "NAs"?
Thanks in advance!!
Upvotes: 1
Views: 233
Reputation: 2000
Try:
comb_region <- function(df)
{
df[,6]=ifelse(is.na(df[,6]), df[,8], df[,6]);
df[,8]=ifelse(is.na(df[,8]), df[,6], df[,8]);
return(df)
}
filled_df=comb_region(df_with_nas)
Direct comparisons to NA typically do not work: use is.na instead.
And notice that the region_merge data.frame you are passing to comb_region only has two columns.
Upvotes: 1