Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204742

Conditional Subset, Manipulate and Replace

$
0
0

Following on from a previous question here I extracted the following data.frame

DF <- data.frame(A =c("One","Two","Three","Four","Five"),
                 B=c(1,1,2,2,3),
                 D=c(10,2,3,-5,5))

subset(DF, B %in% c(1,3))
     A B  D
1  One 1 10
2  Two 1  2
5 Five 3  5

but now I want to (for example) multiply the numbers by (say) five and replace them in the original data.frame

The following code

subset(DF, B %in% c(1,3))[,2:3] * 5
   B  D
1  5 50
2  5 10
5 15 25

gives me the numbers I want but how to I get them back to

      A B  D
1   One 5 50
2   Two 5  10
3 Three 2  3
4  Four 2 -5
5  Five 15 25

The answer is staring me in the face (ie the index numbers ... but how do I get to them)?


Viewing all articles
Browse latest Browse all 204742

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>