Entering edit mode
3.7 years ago
Pepitostar
•
0
Hi Biostar users! Im a bit new to R and I want someone to help me with this problem. Im trying to solve it using loop function in R, unfortunately, I cant do it. Please help, I'm solving this almost a day. In end I want to have an output file that is similar to the given example below. Thanks in advance.
df1 <- data.frame(stringsAsFactors = FALSE,
id = c('13B1', '13B2', '13B3', '13B4', '13B5', '13B6', '13B7', '13B8', '13B9', '13B10'),
A1 = c('ABB', 'CD', 'NA', 'CD', 'CD', 'CD', 'ABB', 'CD', 'CD', 'CD'),
A2 = c('ASD', 'ASD', 'ASD', 'ASD', 'ASD', 'ASD', 'ADD', 'ASD', 'ASD', 'ASD'),
A3 = c('WWW', 'WWW', 'NA', 'WWW', 'NA', 'WWW', 'NA', 'NA', 'XX', 'NA'),
A4 = c('QRS', 'QRS', 'QRS', 'QRS', 'QRS', 'QRS', 'QRS', 'QRS', 'QRS', 'QRS'),
A5 = c('HMM', 'HMM', 'HMM', 'HMM', 'HMM', 'HMM', 'HMM', 'HMM', 'HMM', 'ABB')
)
df2 <- data.frame(stringsAsFactors = FALSE,
Remark = c('Good', 'Bad'),
A1 = c('ABB','CD'),
A4 = c('QRS','FST'),
A3 = c('WWW','XX'),
A5 = c('HMM','ABB'),
A2 = c('ADD','ASD')
)
output <- data.frame(stringsAsFactors = FALSE,
id = c('13B1', '13B2', '13B3', '13B4', '13B5', '13B6', '13B7', '13B8', '13B9', '13B10'),
A1 = c('Good', 'Bad', 'NA', 'Bad', 'Bad', 'Bad', 'Good', 'Bad', 'Bad', 'Bad'),
A2 = c('Bad', 'Bad', 'Bad', 'Bad', 'Bad', 'Bad', 'Good', 'Bad', 'Bad', 'Bad'),
A3 = c('Good', 'Good', 'NA', 'Good', 'NA', 'Good', 'NA', 'NA', 'Bad', 'NA'),
A4 = c('Good', 'Good', 'Good', 'Good', 'Good', 'Good', 'Good', 'Good', 'Good', 'Good'),
A5 = c('Good', 'Good', 'Good', 'Good', 'Good', 'Good', 'Good', 'Good', 'Good', 'Bad')
)
OMG! I tried and it works. You are heaven-sent. Thanks a lot man for this clean and beautiful codes.
Note to the viewers: I have a difficulty of making this work due to the "pivot_longer" or tidyr function. You have to install the latest version of R to make this work. Mine was 3.5.3 :)
Glad it worked!
pivot_longer
andpivot_wider
are from tidyr version 1.1.0 released this May 2020. These functions are replacements forgather
andspread
respectively.