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

performing mathematical functions across a data_frame

$
0
0

I have the following df. I would like to group by ID and do Fy=(Fyb+Fya)/2 for every column that ends with charts. If any of the two values is NA, an NA output would do.

DATA

df <- structure(list(ID = c("51-07519", "51-07519", "51-07522", "51-07522", 
"51-07525", "51-07525"), name = c("Fya", "Fyb", "Fya", "Fyb", 
"Fya", "Fyb"), serology_charts = c("0", "0", "0", "0", NA, NA
), antibodies_charts = c(NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_), bioarray_charts = c(NA, 
NA, "0", "0", NA, NA), others_charts = c(NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
  ID       name  serology_charts antibodies_charts bioarray_charts others_charts
  <chr>    <chr> <chr>           <chr>             <chr>           <chr>        
1 51-07519 Fya   0               NA                NA              NA           
2 51-07519 Fyb   0               NA                NA              NA           
3 51-07522 Fya   0               NA                0               NA           
4 51-07522 Fyb   0               NA                0               NA           
5 51-07525 Fya   NA              NA                NA              NA           
6 51-07525 Fyb   NA              NA                NA              NA

DESIRED OUTPUT

   ID       name  serology_charts antibodies_charts bioarray_charts others_charts
  <chr>    <chr> <chr>           <chr>             <chr>           <chr>        
1 51-07519 Fy   0               NA                NA              NA           
2 51-07522 Fy   0               NA                0               NA           
3 51-07525 Fy   NA              NA                NA              NA           

Viewing all articles
Browse latest Browse all 201919

Trending Articles



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