I have a Dataset and I want to plot a specific row from it.

I manged to do it in a crude way like this:

person7_loss = c(weigh_loss$Jan[weigh_loss$Individuals == "Person 7"], 
             weigh_loss$Feb[weigh_loss$Individuals == "Person 7"],
             weigh_loss$Mar[weigh_loss$Individuals == "Person 7"],
             weigh_loss$Apr[weigh_loss$Individuals == "Person 7"],
             weigh_loss$Mai[weigh_loss$Individuals == "Person 7"],
             weigh_loss$Jun[weigh_loss$Individuals == "Person 7"],
             weigh_loss$Jul[weigh_loss$Individuals == "Person 7"],
             weigh_loss$Aug[weigh_loss$Individuals == "Person 7"],
             weigh_loss$Sep[weigh_loss$Individuals == "Person 7"],
             weigh_loss$Oct[weigh_loss$Individuals == "Person 7"],
             weigh_loss$Nov[weigh_loss$Individuals == "Person 7"],
             weigh_loss$Des[weigh_loss$Individuals == "Person 7"])

But I figure that there's a simpler way to do it.

I imagined something like:

person1 = weigh_loss[1,5:16]
plot(x = person1, type = "h", ylim = c(0, 120), ylab = "Þyngd",
     xlab = "Mánuður")

1 Answers

0
akrun On

A simpler option would be to unlist the columns after filtering the rows

person7_loss <- unlist(weigh_loss[weigh_loss$Individuals == "Person 7", nm1])

where 'nm1' is the columns of interest

nm1 <- 5:16

If there are multiple rows, then transpose the filtered output and convert it to a vector to keep the order

c(t(weigh_loss[weigh_loss$Individuals == "Person 7",nm1]))