I have some data in the format found in the code chunk.
dat <- data.frame(fruit=c('apple',NA,NA,NA,NA,NA,NA,NA,NA,
'pear',NA,NA,NA,NA,NA,NA,NA,NA,
'orange',NA,NA,NA,NA,NA,NA,NA,NA),
fertiliser=c('phosphorus',NA,NA,
'potassium',NA,NA,
'Nitrogen',NA,NA,
'phosphorus',NA,NA,
'potassium',NA,NA,
'Nitrogen',NA,NA,
'phosphorus',NA,NA,
'potassium',NA,NA,
'Nitrogen',NA,NA),
method=rep(c('a','b','c'),9),
no_of_fruits=c(rep(5,3),rep(2,3),rep(6,3),rep(8,3),rep(7,3),rep(4,3),rep(3,3),rep(1,3),rep(9,3)),
mean=c(rnorm(27, 50, 20)),
lower_ci=c(rnorm(27,20,20)),
upper_ci=c(rnorm(27,80,20)),
pval=c(rnorm(27,0.005,0.001)),
x=c(rnorm(1,0.004,0.05),NA,NA,
rnorm(1,0.004,0.05),NA,NA,
rnorm(1,0.004,0.05),NA,NA,
rnorm(1,0.004,0.05),NA,NA,
rnorm(1,0.004,0.05),NA,NA,
rnorm(1,0.004,0.05),NA,NA,
rnorm(1,0.004,0.05),NA,NA,
rnorm(1,0.004,0.05),NA,NA,
rnorm(1,0.004,0.05),NA,NA),
y=c(rnorm(1,0.004,0.0003),NA,NA,
rnorm(1,0.004,0.0003),NA,NA,
rnorm(1,0.004,0.0003),NA,NA,
rnorm(1,0.004,0.0003),NA,NA,
rnorm(1,0.004,0.0003),NA,NA,
rnorm(1,0.004,0.0003),NA,NA,
rnorm(1,0.004,0.0003),NA,NA,
rnorm(1,0.004,0.0003),NA,NA,
rnorm(1,0.004,0.0003),NA,NA))
There are 27 rows of data in the above dataframe, but I'd like to be able to have 6 rows in total in the new dataset: 6 fruit rows, and 3 fertiliser rows for each fruit row.
To turn the data into long format, I want to have each cell in the method row in the original dat
dataframe to be a separate column, so each individual row in the new dataset would be:
| fruit | fertiliser | no_of_fruits | a_mean | a_lower_ci | a_upper_ci | a_pval | a_x | a_y | b_mean | b_lower_ci | b_upper_ci | b_pval | b_x | b_y | c_mean | c_lower_ci | c_upper_ci | c_pval | c_x | c_y |
Hope this makes sense!
I tried using for loops to tackle this, but my attempt seems quite clumsy...
library(tidyverse)
methods <- unique(dat$method)
fert <- unique(dat$fertiliser) # with NAs
fert <- na.omit(fert) # without NAs
fru <- unique(dat$fruit) # with NAs
fru <- na.omit(fru) # without NAs
dat_2 <- data.frame()
longer_row <- NULL
p = 1
# for loop to go through each row in finalised_results_children
for(x in p:length(dat)){
# the above for loop goes through each fruit:
# apple, pear, orange
for(a in 1:length(methods)){
# the above for loop goes through each of 3 methods:
# a,b,c
method = dat[a,3]
results=dat[x,4:8]
# rename columns to e.g. 'a_mean'
columns = colnames(dat[,4:8]) %>%
str_replace_all(., ' ', '_')
columns = paste(method, columns, sep = '_')
colnames(results) = columns
# this seems to be where the problem is!
# we want to create a (1x(5*3 methods) + x + y) row
if(is.null(longer_row)){
longer_row = results
longer_row = cbind(data.frame(fruit=fru[x]), data.frame(fertiliser=fer[x]), longer_row, dat[x, 9:10])
}else{
longer_row = cbind(longer_row, results)
}
}
#
dat_2 <- rbind(dat_2, longer_row)
longer_row = NULL
p = p+3
}
This is what I get. Any help would be much appreciated!