开发者

R Using a for() loop to fill one dataframe with another

I have two dataframes and I wish to insert the values of one dataframe into another (let's call them DF1 and DF2).

DF1 consists of 2 columns 1 and 2. Column 1 (col1) contains characters a to z and col2 has values associated with each character (from开发者_如何转开发 a to z)

DF2 is a dataframe with 3 columns. The first two consist of every combination of DF1$col1 so: aa ab ac ad etc; where the first letter is in col1 and the second letter is in col2

I want to create a simple mathematical model utilizing the values in DF1$col2 to see the outcomes of every possible combination of objects in DF1$col1 The first step I wanted to do is to transfer values from DF1$col2 to DF2$col3 (values from DF2$col3 should be associated to values in DF2col1), but that's where I'm stuck. I currently have

for(j in 1:length(DF2$col1))
{
  ## this part is to use the characters in DF2$col1 as an input 
  ## to yield the output for DF2$col3--
  input=c(DF2$col1)[j]

  ## This is supposed to use the values found in DF1$col2 to fill in DF2$col3

  g=DF1[(DF1$col2==input),"pred"]

  ## This is so that the values will fill in DF2$col3--
  DF2$col3=g
}

When I run this, DF2$col3 will be filled up with the same value for a specific character from DF1 (e.g. DF2$col3 will have all the rows filled with the value associated with character "a" from DF1) What exactly am I doing wrong?

Thanks a bunch for your time


You should really use merge for this as @Aaron suggested in his comment above, but if you insist on writing your own loop, than you have the problem in your last line, as you assign g value to the whole col3 column. You should use the j index there also, like:

for(j in 1:length(DF2$col1))
{
  DF2$col3[j] = DF1[(which(DF1$col2 == DF2$col1[j]), "pred"]
}

If this would not work out, than please also post some sample database to be able to help in more details (as I do not know, but have a gues what could be "pred").


It sounds like what you are trying to do is a simple join, that is, match DF1$col1 to DF2$col1 and copy the corresponding value from DF1$col2 into DF2$col3. Try this:

DF1 <- data.frame(col1=letters, col2=1:26, stringsAsFactors=FALSE)
DF2 <- expand.grid(col1=letters, col2=letters, stringsAsFactors=FALSE)
DF2$col3 <- DF1$col2[match(DF2$col1,  DF1$col1)]

This uses the function match(), which, as the documentation states, "returns a vector of the positions of (first) matches of its first argument in its second." The values you have in DF1$col1 are unique, so there will not be any problem with this method.

As a side note, in R it is usually better to vectorize your work rather than using explicit loops.


Not sure I fully understood your question, but you can try this:

df1 <- data.frame(col1=letters[1:26], col2=sample(1:100, 26))
df2 <- with(df1, expand.grid(col1=col1, col2=col1))
df2$col3 <- df1$col2  

The last command use recycling (it could be writtent as rep(df1$col2, 26) as well).

The results are shown below:

> head(df1, n=3)
  col1 col2
1    a   68
2    b   73
3    c   45
> tail(df1, n=3)
   col1 col2
24    x   22
25    y    4
26    z   17
> head(df2, n=3)
  col1 col2 col3
1    a    a   68
2    b    a   73
3    c    a   45
> tail(df2, n=3)
    col1 col2 col3
674    x    z   22
675    y    z    4
676    z    z   17
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜