Set only selected rows based on "YES" or "NO" in the column

I would like to matplotselect only selected rows from data frames. In both datasets you can find the "YES" or "NO" column. I would like matplotonly the rows (of course, the columns with values) that have YESin the last column. Let me show you the data first, and later I will give a few more explanations of what I expect to achieve.

> dput(c)
structure(list(Fr1 = c(0.2, 0, 0, 0, 0, 0), Fr2 = c(0.7, 0, 0, 
0, 0, 0), Fr3 = c(1, 0.35, 0, 0, 0, 0), Fr4 = c(0.1, 1, 0, 0, 
0.5, 0), Fr5 = c(0, 0.4, 0, 0, 1, 0), Fr6 = c(0, 0, 0, 0, 0.3, 
0), Fr7 = c(0, 0, 0, 0.7, 0, 0), Fr8 = c(0, 0, 0, 1, 0, 0), Fr9 = c(0, 
0, 0, 1, 0, 0), Fr10 = c(0, 0, 0, 0.65, 0, 0.7), Fr11 = c(0, 
0, 0, 0.2, 0, 1), w = structure(c(2L, 2L, 1L, 1L, 1L, 1L), .Label = c("NO", 
"YES"), class = "factor")), .Names = c("Fr1", "Fr2", "Fr3", "Fr4", 
"Fr5", "Fr6", "Fr7", "Fr8", "Fr9", "Fr10", "Fr11", "w"), row.names = c("Mazda RX4", 
"Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive", "Hornet Sportabout", 
"Valiant"), class = "data.frame")


> dput(d)
structure(list(Fr1 = c(1, 0, 0, 0, 0, 0), Fr2 = c(0.7, 0, 0, 
0, 0, 0), Fr3 = c(0.2, 0, 0, 0, 0, 0), Fr4 = c(0.1, 0, 0, 0, 
0.5, 0), Fr5 = c(0, 0.1, 0, 0, 1, 0), Fr6 = c(0, 0, 0, 0, 0.3, 
0), Fr7 = c(0, 0.8, 0, 0.7, 0, 0), Fr8 = c(0, 1, 0, 1, 0, 0), 
    Fr9 = c(0, 0.3, 0, 1, 0, 0), Fr10 = c(0, 0, 0, 0.65, 0, 0.7
    ), Fr11 = c(0, 0, 0, 0.2, 0, 1), w = structure(c(2L, 2L, 
    1L, 1L, 1L, 1L), .Label = c("NO", "YES"), class = "factor")), .Names = c("Fr1", 
"Fr2", "Fr3", "Fr4", "Fr5", "Fr6", "Fr7", "Fr8", "Fr9", "Fr10", 
"Fr11", "w"), row.names = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710", 
"Hornet 4 Drive", "Hornet Sportabout", "Valiant"), class = "data.frame")

What these tables look like:

                  Fr1 Fr2  Fr3 Fr4 Fr5 Fr6 Fr7 Fr8 Fr9 Fr10 Fr11   w
Mazda RX4         0.2 0.7 1.00 0.1 0.0 0.0 0.0   0   0 0.00  0.0 YES
Mazda RX4 Wag     0.0 0.0 0.35 1.0 0.4 0.0 0.0   0   0 0.00  0.0 YES
Datsun 710        0.0 0.0 0.00 0.0 0.0 0.0 0.0   0   0 0.00  0.0  NO
Hornet 4 Drive    0.0 0.0 0.00 0.0 0.0 0.0 0.7   1   1 0.65  0.2  NO
Hornet Sportabout 0.0 0.0 0.00 0.5 1.0 0.3 0.0   0   0 0.00  0.0  NO
Valiant           0.0 0.0 0.00 0.0 0.0 0.0 0.0   0   0 0.70  1.0  NO

                  Fr1 Fr2 Fr3 Fr4 Fr5 Fr6 Fr7 Fr8 Fr9 Fr10 Fr11   w
Mazda RX4           1 0.7 0.2 0.1 0.0 0.0 0.0   0 0.0 0.00  0.0 YES
Mazda RX4 Wag       0 0.0 0.0 0.0 0.1 0.0 0.8   1 0.3 0.00  0.0 YES
Datsun 710          0 0.0 0.0 0.0 0.0 0.0 0.0   0 0.0 0.00  0.0  NO
Hornet 4 Drive      0 0.0 0.0 0.0 0.0 0.0 0.7   1 1.0 0.65  0.2  NO
Hornet Sportabout   0 0.0 0.0 0.5 1.0 0.3 0.0   0 0.0 0.00  0.0  NO
Valiant             0 0.0 0.0 0.0 0.0 0.0 0.0   0 0.0 0.70  1.0  NO

, matplot . - pdf matplot SAME GRAPH . 2 , , ​​ .

, : Output

+4
3

, c .

, ( ) . , , , , .

, mapply, .

, , ( : c cc d is dd). mapply:

# xx is the row of cc you'd like to plot
# yy is the row of dd you'd like to plot
myfun <- function(xx, yy) {
    subCC <- cc[xx, -ncol(cc)]
    subDD <- dd[yy, -ncol(dd)]
    dat <- t(rbind(subCC, subDD))
    matplot(dat, type = "l", lty = c(2, 5), ylab = "Intensity",
        xlab = "Fraction size")
}

,

ccYes <- which(cc[, ncol(cc)] %in% "YES")
ddYes <- which(dd[, ncol(dd)] %in% "YES")

pdf , , mapply

pdf("lines.pdf")
mapply(myfun, ccYes, ddYes)
dev.off()
+3

. , w = YES, R :

df1y <- df1[which(df1$w == "YES"), 1:(ncol(df1) - 1)]
df2y <- df2[which(df2$w == "YES"), 1:(ncol(df2) - 1)]

df1 - dput, df2 - dput, . , w = YES , df1y/df2y (w), NA s.

, . , , , , - .

, .

+1

-, df - , c, R.

Secondly, you can multiply your df according to @Phil's suggestion. As soon as you have this, I assume that you get an error message when you try matplot(df1y[1,],df2y[1,])because of several reasons: firstly, you left the coefficient (yes / no) of the last column and force NAs to force. Secondly, I think you need rbindto correctly display what you want it like this:

matplot(t(rbind(df1y[1,1:11],df2y[1,1:11])),type="l")

Here you can create a loop to create each line in a separate graph.

WITH

+1
source

All Articles