0

I'm very new to R, so please excuse my potentially noob question.

I have data from 23 individuals of hormone concentrations collected hourly - I've interpolated between hourly collections to get concentrations between 2.0 - 15pg/ml at intervals of 0.1 : this equals to 131 rows of data per individual.

Some individials' concentrations, however, don't go beyond 6.0 pg/ml (for example) which means I have dataframes of unequal number of rows across individials. I need all individuals to have 131 rows for the next step where I combine all the data.

I've tried to create a dataframe of NAs with 131 rows and two coloumns, and then add the individual's interplotated data into the NA dataframe - so that the end result is a 131 row data from with missing data as NA - but it's not going so well.

interp_saliva_002_x <- as.tibble(matrix(, nrow = 131, ncol = 1))
interp_sequence <- as.numeric(seq(2,15,.1))
interp_saliva_002_x[1] <- interp_sequence
colnames(interp_saliva_002_x)[1] <- "saliva_conc"

test <- left_join(interp_saliva_002_x, interp_saliva_002, by "saliva_conc")

Can you help me to understand where I'm going wrong or is there a more logical way to do this?

Thank you!

Erty Seidohl
  • 4,487
  • 3
  • 33
  • 45
S.Sleep
  • 1
  • 1
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Maybe checkout the [complete()](https://tidyr.tidyverse.org/reference/complete.html) function from `tidyr` – MrFlick Sep 18 '18 at 21:07
  • 1
    A reproducible example would be very helpful. We need to know what you have and what it's current form is? Are you copying in from a clipboard? From a CSV file? Other? In R you usually don't create a dataframe full of spacers and then fill it (like you might do in Excel). But we'd have to see a minimal reproducible example to be able to point you in a direction. – Adam Sampson Sep 18 '18 at 21:26

1 Answers1

3

Lets assume you have 3 vectors with different lengths:

A<-seq(1,5); B<-seq(2,8); C<-seq(3,5)

Change the length of the vectors to the length that you want (in your case it's 131, I picked 7 for simplicity):

length(A)<-7; length(B)<-7; length(C)<-7 #this replaces all the missing values to NA 

Next you can cbind the vectors to a matrix:

 m <-cbind(A,B,C)
#      A B  C
#[1,]  1 2  3
#[2,]  2 3  4
#[3,]  3 4  5
#[4,]  4 5 NA
#[5,]  5 6 NA
#[6,] NA 7 NA
#[7,] NA 8 NA

You can also change your matrix to a dataframe:

df<-as.data.frame(m)    
Shirin Yavari
  • 626
  • 4
  • 6