I have a data frame with firstname, lastname and I want to permutate them but ONLY for the rows that have values. There are many null fields and I don't want reorder them so that there is ever a firstname value without a lastname value. Ex:
number<- c(1,2,3,4,5)
firstname<- c('','Eddie','Edward','','Edurardo')
lastname <- c('','Vedder', 'Van Halen', '', 'Norton')
permtest <- data.frame(number,firstname,lastname)
permtest
number firstname lastname
1 1
2 2 Eddie Vedder
3 3 Edward Van Halen
4 4
5 5 Eduardo Norton
Expected results would be:
But not:
or:
I tried the transform function but it didn't work:
permtest2 <- transform( permtest2, firstname = sample(firstname,lastname) )
What helps is shuffling only the nonempty entries:
permtest$lastname[permtest$lastname != ''] <- sample(permtest$lastname[permtest$lastname != ''])
permtest
# number firstname lastname
# 1 1
# 2 2 Eddie Van Halen
# 3 3 Edward Vedder
# 4 4
# 5 5 Edurardo Norton