0

I have the following dataframe:

class       SyntacticDiversityTimeLine_T1   SyntacticDiversityTimeLine_T2   SyntacticDiversityTimeLine_T3   SyntacticDiversityTimeLine_T4   SyntacticDiversityTimeLine_T5   SyntacticDiversityTimeLine_T6
FieldWriter 0.022180734                     0.037161135                     0.0333365818    0.0293807746    0.0273657335    0.0218952546
ClassWriter 0.0220335244                    0.0285103085    0.0345992136    0.0323371822    0.0303270617    0.0305631595
ClassReader 0.0287826545                    0.0287826545    0.0287826545    0.0310223776    0.0337064784    0.0322547359
Memory      0.0131923306                    0.0131923306    0.0131923306    0.0173490644    0.0216174768    0.0258896409
Evaluation  0.0128698165                    0.0128698165    0.0145027452    0.0208119061    0.0285975251    0.0318768479
ICSSearch   0.0610376193                    0.0610642137    0.0577870861    0.0497895971    0.0426232686    0.0350948706
ModelReader 0.0295225239                    0.0363729355    0.0356058584    0.0350546489    0.0343149581    0.0334304414

I would like to replace the values in the columns SyntacticDiversityTimeLine_T1:SyntacticDiversityTimeLine_T6 with values from another dataframe. Note that in reality I have data from SyntacticDiversityTimeLine_T1:SyntacticDiversityTimeLine_T70

I tried something like this but didn't work:

df[df$SyntacticDiversityTimeLine_T1:df$SyntacticDiversityTimeLine_T70] <- otherdf[otherdf$SyntacticDiversityTimeLine_T1:otherdf$SyntacticDiversityTimeLine_T70]

How can I do that?

Adam Amin
  • 1,406
  • 2
  • 11
  • 23
  • 1
    For those people who are giving downvotes, I wonder why is that? I am not an expert in R language and I faced a problem where I need help from experts. What seems to be the problem????? – Adam Amin Jul 22 '20 at 21:27
  • 1
    One of the downvotes is mine. Why? I am mildly surprised to hear this question from someone who has been on Stackoverflow for more than a short while. This is not about R, but about the way of asking questions. Please kindly consider reading those links : https://idownvotedbecau.se/nomcve/ https://idownvotedbecau.se/noresearch/. I find it a good general idea / guideline that a question should be asked in a way that costs as little time for the others as possible. Us requiring to try to reproduce your data does not show such an effort and I consider this worth a downvote – tjebo Jul 22 '20 at 21:34
  • I don't know for sure why people are downvoting your question but I suspect they think it has been asked before. Hope this helps. – makeyourownmaker Jul 22 '20 at 21:34

1 Answers1

1

The syntax doesn't seem right.

Sample data:

df1 <- data.frame(
  v1 = c(1,2,3,4,5),
  v2 = c(1,3,5,7,9),
  v3 = c(1,4,7,11,14)
)

df2 <- data.frame(
  x1 = c(0.5,2.5,3.5,4.5,5.5),
  x2 = c(1.5,3.5,5.5,7.5,9.5),
  x3 = c(1.5,4.5,7.5,11.5,14.5)
)

To overwrite the values in df1by values in df2you need to subset on columns, which is done using the comma inside the square brackets:

df1[,2:3] <- df2[,2:3]

Result:

df1
  v1  v2   v3
1  1 1.5  1.5
2  2 3.5  4.5
3  3 5.5  7.5
4  4 7.5 11.5
5  5 9.5 14.5
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34