简体   繁体   中英

Customized merging of dataframe in R

I would like to merge the following data frame, so that each row contains the column name of the data point and the data point.

non.MML X2.MML  X3.MML  X4.MML  X5.MML  X6.7.MML
-13.994 NA  NA  NA  NA  NA
NA  -13.992 NA  NA  NA  NA
NA  NA  -13.984 NA  NA  NA
NA  NA  NA  -13.983 NA  NA
NA  NA  NA  NA  -13.962 NA
NA  NA  NA  NA  NA  NA  -13.907
NA  NA  -1.2    NA  NA  NA
NA  NA  NA  -14.2   NA  NA
NA  NA  NA  NA  -11.01  NA
NA  NA  NA  NA  NA  NA  -17.23

This is what I would like to get:

name    score
non.MML -13.994
X2.MML  -13.992
X3.MML  -13.984
X4.MML  -13.983
X5.MML  -13.962
X6.7.MML    -13.907
X3.MML  -1.2
X4.MML  -14.2
X5.MML  -11.01
X6.7.MML    -17.23

I tried using this, and it gets me close to what I want:

mydata <- data.frame(x=unlist(mydata))

But I get this:

    x
non.MML1    -13.994
X2.MML1 -13.992
X3.MML1 -13.984
X4.MML1 -13.983
X5.MML1 -13.962
X6.7.MML1   -13.907
X3.MML2 -1.2
X4.MML2 -14.2
X5.MML2 -11.01
X6.7.MML2   -17.23

As you can notice the first element of each row is modified with a number because there are multiple repeats. Whats the best way to accomplish my desired output?

Use melt from reshape2 :

reshape2::melt(df, na.rm = TRUE, variable.name = "name", value.name = "score")

#       name   score
#1   non.MML -13.994
#12   X2.MML -13.992
#23   X3.MML -13.984
#27   X3.MML  -1.200
#34   X4.MML -13.983
#38   X4.MML -14.200
#45   X5.MML -13.962
#49   X5.MML -11.010
#56 X6.7.MML -13.907
#60 X6.7.MML -17.230

Or use baseR stack function:

setNames(na.omit(stack(df)), c("score", "name"))

#     score     name
#1  -13.994  non.MML
#12 -13.992   X2.MML
#23 -13.984   X3.MML
#27  -1.200   X3.MML
#34 -13.983   X4.MML
#38 -14.200   X4.MML
#45 -13.962   X5.MML
#49 -11.010   X5.MML
#56 -13.907 X6.7.MML
#60 -17.230 X6.7.MML

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM