0

I have a data frame that looks like this: see data frame here

I need to make a new data frame that takes the values for density from the expr_phenotype column and puts them in the formula: ((density for 4 + density for 68)/(density for 0+4+64+68)*100). How should I go about this?

I know it's easier to make a reproducible example but this is a funky dataset.

2
  • Please do not provide screen shots of your data. Instead, provide it as text by clicking the edit button under your post. Either copy the output of the dput(head(data)) command and paste the output into your question (preferred) or paste the tab delimited data from a spreadsheet program. Please see How to make a great R reproducible example for more information. Commented Apr 21, 2021 at 4:03
  • it's not very clear what you are trying to achieve here based on your description. You might consider use pivot_wider(names_from = expr, values_from = density) to make the expr colum spread out and select the values by their col names. Commented Apr 21, 2021 at 4:16

1 Answer 1

1

We can use dplyr::group_by, then use summarise:

library(dplyr)
data %>%
   group_by(core_x,core_y) %>%
   summarise(result = sum(density[expr_phenotype %in% c(4,68)])/
                      sum(density[expr_phenotype %in% c(0,4,64,68)]) * 100)
## A tibble: 3 x 3
## Groups:   core_x [1]
#  core_x core_y result
#   <int>  <int>  <dbl>
#1      1      1   39.8
#2      1      2   39.1
#3      1      3   51.8

Data (Obtained by OCR, please forgive errors)

data <- structure(list(core_x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), core_y = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L), core = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L), .Label = c("1--1", "1--2", 
"1--3"), class = "factor"), xX = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = "Cc", class = "factor"), phenotype = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "CD163", class = "factor"), 
    expr_phenotype = c(0L, 4L, 64L, 68L, 0L, 4L, 64L, 68L, 0L, 
    4L), count = c(510L, 334L, 1L, 4L, 186L, 116L, 1L, 3L, 196L, 
    210L), density = c(451L, 295L, 1L, 4L, 164L, 103L, 1L, 3L, 
    173L, 186L)), row.names = 3:12, class = "data.frame")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.