You asked a question that leads to asking others to do all your work for you. The question of one particular piece of this project is likely to be more likely to attract an answer. The part you are struggling with is preventing you from starting - this is a very simple programming skill: the ability to break down your problem into small specific steps, solve each one individually, and then combine them again to solve your original problem.
This skill is also very difficult to learn. But you have a good start! You have well specified four groups that your data may fall into:
All values for "data": NA
All values for "data" are identical, no NA
At least 1 value for "Data" is not identical, no NA.
At least 1 value for "Data" is not identical, at least one of them is NS.
Now you need to think about how, if you have only one subset of your data, can you figure out how to determine in R the group (1-4) in which it is located? Below is a sketch of some tools that may be useful for this. Create several subsets and play in the console until you feel the convenience of identifying each group:
(1) All values for datSub$Data
NA
s?
Tools: all
and is.na
(2) Only one unique value, not NA
?
Tools: length
, unique
, is.na
, any
(3) More than one unique value, no NA
s?
Tools: length
, unique
, any
, is.na
(4) More than one unique value of at least one NA
?
Tools: length
, unique
, any
, is.na
It may be possible without using all of these features, but all of them are potentially useful.
As soon as you learn how to determine which group a particular subset should be in, you are ready to transfer this code to the function. My suggestions are to create a new column with a value of 1-4 depending on the group the subset belongs to:
myFun <- function(x){ if (...){ x$grp <- 1 } if (...){ x$grp <- 2 }
Then use ddply
to apply this function to each subset of your data based on Sample
values:
ddply(dat,.(Sample),.fun = myFun)
And finally, let's split this data frame into its new grp
variable:
split(dat,dat$grp)
Hope this general sketch helps you get started. But you will have problems. They do everything. If you encounter certain problems along the way, feel free to ask another question.
In fact, now I see that John posted the answer according to my sketch. However, I will post this answer anyway in the hope that it will help you analyze future problems.