-1

I am trying to bring my data from wide to long format. So far I had 1 row that I wanted to gather. But now my data demand to bring 4 rows into 4 different variables while keeping the rest of the df intact.

I want to bring row X, Y, Miniplotid and Plot into long format and generating df$Plot, df$X, df$Y and df$Miniplot ID.

Data screenshot:

I used this but it works only for one row (the first):

df <- gather(df, key = "cell", value = "cover", X01:ncol(df))
zephryl
  • 14,633
  • 3
  • 11
  • 30
Paschalis
  • 1
  • 1
  • You may try to transpose your dataframe first with `t()` – Julien Feb 13 '23 at 10:51
  • To my information transpose is for matrices. – Paschalis Feb 13 '23 at 10:53
  • Yes, but since you have only numerical values it may be relevant – Julien Feb 13 '23 at 10:57
  • 4
    [Posting images of data is not advised](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors) and `tidyr::gather` and `tidyr::spread` have been superseded by `tidyr::pivot_longer` and `tidyr::pivot_wider` - I would suggest looking into the help functions of those packages – jpsmith Feb 13 '23 at 10:57
  • `as.data.frame(t(df))` – Julien Feb 13 '23 at 10:58
  • 1
    indeed transpose worked out. But I have to do a lot of data manipulation (bring first row as coll names and transform first column into variable. But works out!!! Thank you. Is there is a more efficient way to do it with a command? – Paschalis Feb 13 '23 at 11:00
  • Likely yes, but I think it best to inform that question using actual data_ (whether real or representative/sampled). Please see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info for discussions on the use of `dput`, `data.frame`, and `read.table` for sharing data in a way that we can actually use. – r2evans Feb 14 '23 at 15:50

1 Answers1

0

(This is a dupe of Reshaping data.frame from wide to long format, Transforming wide data to long format with multiple variables. I can't vtc, vote was already cast.)

Sample data:

set.seed(42)
dat <- data.frame(Plot=letters[1:4], X01=runif(4), X01.1=runif(4), X01.2=runif(4), Y01=runif(4), Y01.1=runif(4), Y01.2=runif(4))
dat
#   Plot       X01     X01.1     X01.2       Y01     Y01.1     Y01.2
# 1    a 0.9148060 0.6417455 0.6569923 0.9346722 0.9782264 0.9040314
# 2    b 0.9370754 0.5190959 0.7050648 0.2554288 0.1174874 0.1387102
# 3    c 0.2861395 0.7365883 0.4577418 0.4622928 0.4749971 0.9888917
# 4    d 0.8304476 0.1346666 0.7191123 0.9400145 0.5603327 0.9466682

Code

tidyr::pivot_longer(dat, -Plot, names_pattern = "([XY])(.*)", names_to = c(".value", "miniplot"))
# # A tibble: 12 × 4
#    Plot  miniplot     X     Y
#    <chr> <chr>    <dbl> <dbl>
#  1 a     01       0.915 0.935
#  2 a     01.1     0.642 0.978
#  3 a     01.2     0.657 0.904
#  4 b     01       0.937 0.255
#  5 b     01.1     0.519 0.117
#  6 b     01.2     0.705 0.139
#  7 c     01       0.286 0.462
#  8 c     01.1     0.737 0.475
#  9 c     01.2     0.458 0.989
# 10 d     01       0.830 0.940
# 11 d     01.1     0.135 0.560
# 12 d     01.2     0.719 0.947
r2evans
  • 141,215
  • 6
  • 77
  • 149