r - create a list from duplicated data in columns in the same dataframe in RStudio -
i have several columns in same data frame lists location names, column headings differ slightly
- location 1 (adelaide, sydney, perth)
- location 2 (perth, darwin, adelaide)
- location 3 (brisbane, adelaide, melbourne)
i want 1 location column combines 3 of these columns keeps unique location names.
for example, final column list - location (adelaide, sydney, perth, darwin, brisbane, melbourne)
if in dataframe thei gives reduced factor result,
> unique(unlist(dat)) [1] adelaide sydney perth darwin brisbane melbourne levels: adelaide perth sydney darwin brisbane melbourne
which if wanted dataframe simple enough:
newdat <- data.frame(location1 = unique(unlist(dat)) ) > newdat location1 1 adelaide 2 sydney 3 perth 4 darwin 5 brisbane 6 melbourne
and as.character
turn character vector.
test object:
dat <- data.frame( location1 = c('adelaide', 'sydney', 'perth'), location2= c('perth', 'darwin', 'adelaide'), location3 =c('brisbane', 'adelaide', 'melbourne'))
Comments
Post a Comment