如何根据R中纵向情况下不同变量的状态按一个变量分组?
我是R的新手,所以请对我放松点。。。我有一些纵向数据看起来像 基本上,我试图找到一种方法来获得一个表,其中a)包含所有完整数据的唯一案例的数量,b)包含至少一个不完整或缺失数据的唯一案例的数量。理想的最终结果是如何根据R中纵向情况下不同变量的状态按一个变量分组?,r,dplyr,any,R,Dplyr,Any,我是R的新手,所以请对我放松点。。。我有一些纵向数据看起来像 基本上,我试图找到一种方法来获得一个表,其中a)包含所有完整数据的唯一案例的数量,b)包含至少一个不完整或缺失数据的唯一案例的数量。理想的最终结果是 df<- df %>% group_by(Location) df1<- df %>% group_by(any(Completion_status=='Incomplete' | 'Missing')) df%分组依据(位置) df1%分组依据(任何(完成
df<- df %>% group_by(Location)
df1<- df %>% group_by(any(Completion_status=='Incomplete' | 'Missing'))
df%分组依据(位置)
df1%分组依据(任何(完成状态=‘未完成’|‘缺少’)
不确定您想要什么,因为您的请求和所需的输出之间似乎存在一些不一致的地方,但是让我们尝试一下,您似乎需要一种频率表,您可以使用基本R进行管理。在答案的底部,您可以找到一些与您的数据类似的数据
# You have two cases, the Complete, and the other, so here a new column about it:
data$case <- ifelse(data$Completion_status =='Complete','Complete', 'MorIn')
# now a frequency table about them: if you want a data.frame, here we go
result <- as.data.frame.matrix(table(data$Location,data$case))
# now the location as a new column rather than the rownames
result$Location <- rownames(result)
# and lastly a data.frame with the final results: note that you can change the names
# of the columns but if you want spaces maybe a tibble is better
result <- data.frame(Location = result$Location,
`Number.complete` = result$Complete,
`Number.incomplete.missing` = result$MorIn)
result
Location Number.complete Number.incomplete.missing
1 London 0 1
2 Los Angeles 0 1
3 Paris 3 1
4 Phoenix 0 2
5 Toronto 1 1
#您有两个案例,一个是完整案例,另一个是其他案例,因此这里有一个关于它的新专栏:
数据$case%
选择(3,1,2)%>%
`Colnames请不要发布数据的图片。读取并共享dput(CT)
的输出。这将使其他人更容易帮助您。这里有一些错误,但我也无法理解您的输出。正如markus所说,请发布可复制/粘贴且可导入的文本数据,而不是难以处理的数据图片。另外,请解释你所说的group\u by(Data.Access.group)
是什么意思,你的任何照片中都没有显示该栏。我不知道你是怎么知道我在找什么的,但我很感谢你知道。这正是我需要的!!!非常感谢你!!!
data %>%
mutate(case = ifelse(data$Completion_status =='Complete','Complete', 'MorIn')) %>%
do( as.data.frame.matrix(table(.$Location,.$case))) %>%
mutate(Location = rownames(.)) %>%
select(3,1,2) %>%
`colnames<-`(c("Location","Number of complete ", "Number of incomplete or"))
Location Number of complete Number of incomplete or
1 London 0 1
2 Los Angeles 0 1
3 Paris 3 1
4 Phoenix 0 2
5 Toronto 1 1
# here your data (next time try to put them in an usable way in the question)
data <- data.frame( ID = c("A1","A1","A2","A2","B1","C1","C2","D1","D2","E1"),
Location = c('Paris','Paris','Paris','Paris','London','Toronto','Toronto','Phoenix','Phoenix','Los Angeles'),
Completion_status = c('Complete','Complete','Incomplete','Complete','Incomplete','Missing',
'Complete','Incomplete','Incomplete','Missing'))