[英]How to extract observations from a data frame and make a table showing observations, column name, and row name?
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.
然后消除其中包含NA的值。 Currently the df looks like:
当前df如下所示:
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. 我试过使用
!is.na()
但它不会消除值并在当前状态下构造df。 Currently I have it organized by using chart_df %>% group_by(row.names(chart_df))
but doesn't quite organize it correctly. 目前,我已经通过使用
chart_df %>% group_by(row.names(chart_df))
组织它,但是并没有正确地组织它。
What I would want to have is a df that looks like: 我想要的是一个看起来像这样的df:
Condition Column Row
NA Well1 A
2Mg Well1 B
3Mg Well1 C
4Mg Well1 D
I think you're looking for melt
我想你正在寻找
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
更新 :当
id.vars
( Flask
列)是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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.