I'm trying to extract observations from a data frame and create a new data frame that shows the observations in one column, corresponding columns in another column, and corresponding rows in another column. Then eliminate the values that have NA in them. Currently the df looks like:
Flask Well 1 Well 2
A NA NA
B 2Mg Control
C 3Mg Control
D 4Mg Control
E NA NA
I've tried using !is.na()
but it wont eliminate the values and structure the df in the current state. Currently I have it organized by using chart_df %>% group_by(row.names(chart_df))
but doesn't quite organize it correctly.
What I would want to have is a df that looks like:
Condition Column Row
NA Well1 A
2Mg Well1 B
3Mg Well1 C
4Mg Well1 D
I think you're looking for melt
# the data
my_df <- read.table(text = "Flask Well_1 Well_2
A NA NA
B 2Mg Control
C 3Mg Control
D 4Mg Control
E NA NA", header = TRUE)
library(reshape2)
melt(my_df , id.vars = 'Flask') %>%
setNames(c('Row', 'Column', 'Condition')) %>%
na.omit()
update : when the id.vars
( Flask
column) is the rownames
# the data
my_df <- my_df[c('Well_1', 'Well_2')] %>%
as.data.frame(row.names = my_df$Flask)
my_df %>%
as.matrix() %>% melt() %>% na.omit()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.