I have 23 data frames each containing ~20 observations over 200 variables and another data frame containing 13 variables and 23 observerations. These 13 variables hold information about the 23 data frames.
What I'm trying to do is to find a way to add the information from the lone data frame to each corresponding data frame in the list of 23, so that each observation in one out of the 23 data frames will hold the same value (e.g. the timepoint the whole data frame has been recorded). The first line in the lone data frame corresponds to the information for the first data frame of the list of 23 and so on.
ls()
[1] "df1" "df10" "df11" "df12" "df13" "df14" "df15" "df16" "df17" "df18" "df19" "df2"
[13] "df20" "df21" "df22" "df23" "df3" "df4" "df5" "df6" "df7" "df8" "df9" "i"
[25] "lf"
After some research I tried putting this into a list but realized that I have actually no idea in which order the list stores my data. I know that df1 matches row one of the lone frame "lf" (and if the list just flips things I'll match it the wrong way).
So on a single example I tried combine which worked somewhat (but not all too well):
> testdf <- c(df1,lf[1,])
> is.data.frame(testdf)
[1] FALSE
> testdf <- as.data.frame(testdf)
> is.data.frame(testdf)
[1] TRUE
At first it was a list, but using as.data.frame and having a look at the specific columns using View() it was the result I need. e.g. a new column at the end of the frame containing a variable like "time" that has values 13:37 for all observations in "df1".
Next I tried a loop...
for (i in 1:23){
+ assign(paste0("df",i), cbind(paste0("df",i),lf[i,], row.names = NULL))
+ }
...basically just trying to do what I did first multiple times (as.data.frame() is missing here, but it doesn't change a thing). What happens is that each data frame now only has 1 Observeration containing 13 variables I wanted to add at the end of the original frame.
After that everything has gone to s*** basically. I've tried to google for hours, but couldn't get anything to work really. Mostly I've tried playing around with it as a list until I realized this was a bad idea without getting the order right first (I actually know now how I can get that sorted out but right now I don't have the energy to do that. If you have a solution with a list that contains the name of each data frame as stored in the list, I'm sure I can get up to that point).
EDIT So I tried to make an example and show where I'm coming from. I hope it's more clear. I'm aware that I sadly don't solve it the "R-way" like this, which is why I tried looking at lists and apply a lot, but wasn't able to come up with a solution still.
> #create 3 data frames, 5 observations and 10 variables each
> df1 <- as.data.frame(matrix(rnorm(50, mean = 50, sd = 10), ncol = 10, nrow = 5))
> df2 <- as.data.frame(matrix(rnorm(50, mean = 50, sd = 10), ncol = 10, nrow = 5))
> df3 <- as.data.frame(matrix(rnorm(50, mean = 50, sd = 10), ncol = 10, nrow = 5))
>
> #create lone data frame with 3 observerations (1 per data frame) and 2 variables
> df4 <- as.data.frame(matrix(rnorm(6, mean = 5, sd = 1), ncol = 2, nrow = 3))
>
> #create colnames for better explanation
> cn <- c()
> for (i in 1:12){
+ cn[i] <- paste0("Var",i)
+ }
> colnames(df1) <- cn[1:10]
> colnames(df2) <- cn[1:10]
> colnames(df3) <- cn[1:10]
> colnames(df4) <- cn[11:12]
>
> #working example for 1 out of 3 matches
> #adding the first row of the lone data frame "df4" containing
> #Var11 and Var12 to df1. Result is as desired
> newdf1 <- c(df1,df4[1,])
> as.data.frame(newdf1)
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12
1 52.37538 48.47529 41.93258 45.93547 41.71611 58.86811 40.70888 41.87981 56.80464 49.73488 5.233276 4.417211
2 51.90261 61.72404 44.96621 48.59473 51.61673 51.07525 55.02000 43.48264 34.03446 48.93913 5.233276 4.417211
3 39.85056 48.72688 49.93816 60.41899 54.90524 56.84387 53.92486 55.92178 50.81779 66.03640 5.233276 4.417211
4 41.61915 53.22312 47.96660 50.79573 34.98073 41.81004 46.43976 45.49678 32.48257 58.65475 5.233276 4.417211
5 58.52455 39.70007 51.26386 39.92583 47.08723 31.41743 45.34423 63.06964 61.07181 55.44908 5.233276 4.417211
> df4
Var11 Var12
1 5.233276 4.417211
2 5.309388 5.375850
3 6.342876 5.318077
Really grateful for any help offered :)
PS: My first post here, I hope it's readable.