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

Creating two new columns and removing the source column

$
0
0

I have the following example data:

df <- data.frame(ID=c("A1","A2","A3","A4","A1","A2","A3","A4"),
                 NUM=c(469,586,394,595,398,203,604,809))

I am looking to extract the first value of the NUM column and place it in a new column NUM1, and then on the second occurrence of a NUM value for the same ID, extract that value into a new column NUM2. Finally, I would like to remove the original column. The dataset I have has many more variables and columns in addition to ID and NUM. The following is the desired output.

df1 <- data.frame(ID=c("A1","A2","A3","A4"),NUM1=c(469,586,394,595),NUM2=c(398,203,604,809))

Viewing all articles
Browse latest Browse all 201839

Trending Articles



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