다음은 내가 작업중인 데이터의 작은 덩어리입니다.
data<-structure(list(record_id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19), fracture1_lateral___1 = c(0,
1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), fracture1_medial___1 = c(0,
0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0), fracture1px_lateral___1 = c(0,
1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), fracture1px_medial___1 = c(0,
0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -19L), spec = structure(list(
cols = list(record_id = structure(list(), class = c("collector_double",
"collector")), fracture1_lateral___1 = structure(list(), class = c("collector_double",
"collector")), fracture1_medial___1 = structure(list(), class = c("collector_double",
"collector")), fracture1px_lateral___1 = structure(list(), class = c("collector_double",
"collector")), fracture1px_medial___1 = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
다음은 원래 입력 한 내용의 스크린 샷이며 각 확인란은 고유 한 열이됩니다.
누군가 각 양식에서 "동일한"출력을 확인했는지 확인하고 싶습니다. 즉, 한 양식에서 "Right Condylar"를 클릭하면 다음 사진과 같이 다른 양식에서도 클릭했습니다.
내 데이터에서이 코드는 두 변수가 동일한 것을 말했는지 여부에 대해 TRUE / FALSE 인 새 변수를 생성하여이를 수행 할 수 있다는 것을 알고 있습니다.
data<-data%>%mutate(Same=(fracture1_medial___1==fracture1px_medial___1))
내 문제는 여러 지점에서 해당 변수를 확인해야하고 특별한 규칙이 있다는 것입니다.
In that photo, there are 4 places where it says "Right Condylar" and what I need to know is: if either of the boxes on the left were clicked, then was at least one of the boxes on the right clicked? I.e. if we noticed the scenario in this photo:
Where "fracture1_lateral_1" and "fracture1px_medial" were both clicked, thats fine and would result in a "TRUE" but this scenario below with "fracture1px_medial" and "fracture1px_lateral" would be FALSE.
There's probably 10ish "Right Condylar" variables to compare (5ish on each side), but for instance in that photo the "Right Condylar" variables are:
data$fracture1_medial_1, data$fracture1_lateral_1, data$fracture1px_medial_1, data$fracture1px_lateral_1
Edit per @Ben's comment for clarification:
What I ultimately want would be this column:
In that photo, you can see that if there's a "1" found in at least one of the left two columns and at least one of the right two, it results in a "Yes", or if its all zero's all the way across its a "Yes", but if there are only "1's" on the left side, or the right side.... its a "No"
Any solutions? Tidyverse would be preferable simply because I'm used to it.
Here is a tidyverse
approach.
First, would convert data to long format. That will help in looking at similar groups and subgroups, and would be flexible for different needs when analyzing similar data.
긴 형식이면 group_by
각 행 또는 record_id
을 수행하고 적절한 논리를 사용할 수 있습니다 . 이 경우 "px"및 "px"없음 모두에 대해 "medial"및 "lateral"을보고 있습니다. 논리는 작성되어 있지만 사용 any
방법이나 다른 방법 을 고려할 지름길이있을 수 있습니다 .
요인은 변환 생성 TRUE
하고 FALSE
"예"와 "아니오"로. 최종 right_join
결과는 새 열 결과를 원래 데이터에 연결합니다.
library(tidyverse)
data %>%
pivot_longer(cols = -record_id,
names_to = c("number1", "location", "number2"),
names_pattern = "^fracture(\\d+)(\\w+)___(\\d+)") %>%
separate(location, into = c("px", "location"), sep = "_", remove = TRUE) %>%
group_by(record_id) %>%
summarise(same = ifelse((value[px == "" & location == "lateral"] |
value[px == "" & location == "medial"]) ==
(value[px == "px" & location == "lateral"] |
value[px == "px" & location == "medial"]), "Yes", "No")) %>%
right_join(data)
산출
record_id same fracture1_lateral___1 fracture1_medial___1 fracture1px_lateral___1 fracture1px_medial___1
<dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 1 Yes 0 0 0 0
2 2 Yes 1 0 1 0
3 3 Yes 1 0 0 1
4 4 Yes 0 1 1 0
5 5 Yes 0 1 0 1
6 6 No 0 0 1 1
7 7 No 1 1 0 0
8 8 Yes 0 0 0 0
9 9 Yes 0 0 0 0
10 10 No 1 1 0 0
11 11 Yes 0 0 0 0
12 12 Yes 0 0 0 0
13 13 Yes 0 0 0 0
14 14 Yes 0 0 0 0
15 15 Yes 0 1 0 1
16 16 Yes 0 0 0 0
17 17 Yes 0 0 0 0
18 18 Yes 0 0 0 0
19 19 Yes 0 0 0 0
데이터
data <- structure(list(record_id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19), fracture1_lateral___1 = c(0,
1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), fracture1_medial___1 = c(0,
0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0), fracture1px_lateral___1 = c(0,
1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), fracture1px_medial___1 = c(0,
0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0)), row.names = c(NA,
-19L), class = c("tbl_df", "tbl", "data.frame"))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다