How can I group by one variable in terms of status of a different variable in a longitudinal situation in R?










3















I'm new to R, so please go easy on me... I have some longitudinal data that looks like
this



Basically, I'm trying to find a way to get a table with a) the number of unique cases that have all complete data and b) the number of unique cases that have at least one incomplete or missing data. The end results would ideally be



this



df<- df %>% group_by(Location)
df1<- df %>% group_by(any(Completion_status=='Incomplete' | 'Missing'))









share|improve this question



















  • 1





    Please don't post pictures of your data. Read How to make a great R reproducible example and share the output of dput(CT). This will make it much easier for others to help you.

    – markus
    Nov 14 '18 at 20:46







  • 1





    There are a few bugs here, but I can't understand your output either. Please, as markus says, post text data that is copy/pasteable and importable, not pictures of data that are difficult to work with. Also, please explain what you mean by group_by(Data.Access.Group), that column doesn't show up in any of your pictures.

    – Gregor
    Nov 14 '18 at 20:56















3















I'm new to R, so please go easy on me... I have some longitudinal data that looks like
this



Basically, I'm trying to find a way to get a table with a) the number of unique cases that have all complete data and b) the number of unique cases that have at least one incomplete or missing data. The end results would ideally be



this



df<- df %>% group_by(Location)
df1<- df %>% group_by(any(Completion_status=='Incomplete' | 'Missing'))









share|improve this question



















  • 1





    Please don't post pictures of your data. Read How to make a great R reproducible example and share the output of dput(CT). This will make it much easier for others to help you.

    – markus
    Nov 14 '18 at 20:46







  • 1





    There are a few bugs here, but I can't understand your output either. Please, as markus says, post text data that is copy/pasteable and importable, not pictures of data that are difficult to work with. Also, please explain what you mean by group_by(Data.Access.Group), that column doesn't show up in any of your pictures.

    – Gregor
    Nov 14 '18 at 20:56













3












3








3








I'm new to R, so please go easy on me... I have some longitudinal data that looks like
this



Basically, I'm trying to find a way to get a table with a) the number of unique cases that have all complete data and b) the number of unique cases that have at least one incomplete or missing data. The end results would ideally be



this



df<- df %>% group_by(Location)
df1<- df %>% group_by(any(Completion_status=='Incomplete' | 'Missing'))









share|improve this question
















I'm new to R, so please go easy on me... I have some longitudinal data that looks like
this



Basically, I'm trying to find a way to get a table with a) the number of unique cases that have all complete data and b) the number of unique cases that have at least one incomplete or missing data. The end results would ideally be



this



df<- df %>% group_by(Location)
df1<- df %>% group_by(any(Completion_status=='Incomplete' | 'Missing'))






r dplyr any






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 21:49









s_t

3,47121031




3,47121031










asked Nov 14 '18 at 20:43









HaleyHaley

234




234







  • 1





    Please don't post pictures of your data. Read How to make a great R reproducible example and share the output of dput(CT). This will make it much easier for others to help you.

    – markus
    Nov 14 '18 at 20:46







  • 1





    There are a few bugs here, but I can't understand your output either. Please, as markus says, post text data that is copy/pasteable and importable, not pictures of data that are difficult to work with. Also, please explain what you mean by group_by(Data.Access.Group), that column doesn't show up in any of your pictures.

    – Gregor
    Nov 14 '18 at 20:56












  • 1





    Please don't post pictures of your data. Read How to make a great R reproducible example and share the output of dput(CT). This will make it much easier for others to help you.

    – markus
    Nov 14 '18 at 20:46







  • 1





    There are a few bugs here, but I can't understand your output either. Please, as markus says, post text data that is copy/pasteable and importable, not pictures of data that are difficult to work with. Also, please explain what you mean by group_by(Data.Access.Group), that column doesn't show up in any of your pictures.

    – Gregor
    Nov 14 '18 at 20:56







1




1





Please don't post pictures of your data. Read How to make a great R reproducible example and share the output of dput(CT). This will make it much easier for others to help you.

– markus
Nov 14 '18 at 20:46






Please don't post pictures of your data. Read How to make a great R reproducible example and share the output of dput(CT). This will make it much easier for others to help you.

– markus
Nov 14 '18 at 20:46





1




1





There are a few bugs here, but I can't understand your output either. Please, as markus says, post text data that is copy/pasteable and importable, not pictures of data that are difficult to work with. Also, please explain what you mean by group_by(Data.Access.Group), that column doesn't show up in any of your pictures.

– Gregor
Nov 14 '18 at 20:56





There are a few bugs here, but I can't understand your output either. Please, as markus says, post text data that is copy/pasteable and importable, not pictures of data that are difficult to work with. Also, please explain what you mean by group_by(Data.Access.Group), that column doesn't show up in any of your pictures.

– Gregor
Nov 14 '18 at 20:56












1 Answer
1






active

oldest

votes


















2














Not sure about what you want, because it seems there are something of inconsistent between your request and the desired output, however lets try, it seems you need a kind of frequency table, that you can manage with basic R. At the bottom of the answer you can find some data similar to yours.



# 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


Or if you prefere a dplyr chain:



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



With data:



# 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'))





share|improve this answer

























  • I don't know how you knew what I was looking for, but I sure am thankful you knew. That was exactly what I needed!!! Thank you so much!!!

    – Haley
    Nov 14 '18 at 22:38










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308437%2fhow-can-i-group-by-one-variable-in-terms-of-status-of-a-different-variable-in-a%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Not sure about what you want, because it seems there are something of inconsistent between your request and the desired output, however lets try, it seems you need a kind of frequency table, that you can manage with basic R. At the bottom of the answer you can find some data similar to yours.



# 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


Or if you prefere a dplyr chain:



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



With data:



# 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'))





share|improve this answer

























  • I don't know how you knew what I was looking for, but I sure am thankful you knew. That was exactly what I needed!!! Thank you so much!!!

    – Haley
    Nov 14 '18 at 22:38















2














Not sure about what you want, because it seems there are something of inconsistent between your request and the desired output, however lets try, it seems you need a kind of frequency table, that you can manage with basic R. At the bottom of the answer you can find some data similar to yours.



# 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


Or if you prefere a dplyr chain:



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



With data:



# 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'))





share|improve this answer

























  • I don't know how you knew what I was looking for, but I sure am thankful you knew. That was exactly what I needed!!! Thank you so much!!!

    – Haley
    Nov 14 '18 at 22:38













2












2








2







Not sure about what you want, because it seems there are something of inconsistent between your request and the desired output, however lets try, it seems you need a kind of frequency table, that you can manage with basic R. At the bottom of the answer you can find some data similar to yours.



# 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


Or if you prefere a dplyr chain:



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



With data:



# 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'))





share|improve this answer















Not sure about what you want, because it seems there are something of inconsistent between your request and the desired output, however lets try, it seems you need a kind of frequency table, that you can manage with basic R. At the bottom of the answer you can find some data similar to yours.



# 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


Or if you prefere a dplyr chain:



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



With data:



# 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'))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 22:28

























answered Nov 14 '18 at 22:16









s_ts_t

3,47121031




3,47121031












  • I don't know how you knew what I was looking for, but I sure am thankful you knew. That was exactly what I needed!!! Thank you so much!!!

    – Haley
    Nov 14 '18 at 22:38

















  • I don't know how you knew what I was looking for, but I sure am thankful you knew. That was exactly what I needed!!! Thank you so much!!!

    – Haley
    Nov 14 '18 at 22:38
















I don't know how you knew what I was looking for, but I sure am thankful you knew. That was exactly what I needed!!! Thank you so much!!!

– Haley
Nov 14 '18 at 22:38





I don't know how you knew what I was looking for, but I sure am thankful you knew. That was exactly what I needed!!! Thank you so much!!!

– Haley
Nov 14 '18 at 22:38



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308437%2fhow-can-i-group-by-one-variable-in-terms-of-status-of-a-different-variable-in-a%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3