How to pick specific subjects in R [closed] - r

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
tried to pick up 2 random subjects, but don't know how to do in R
random.subj <- sample(1:max(Data$Id), 2)
rd <- subset(Data$Id, Data$Id==random.subj)
I have a dataset "Data" like
Id
1
1
2
2
3
3
4
4
4
...

Well, in this case random.subj will be a vector of two elements. In that case, doing an equality comparison with == probably isn't want you want because it will just recycle through the shorter list to perform the comparison rather than checking each row for either value as you probably intend.
Also i'm not sure if all your IDs are numerical and sequential. It's better to just take a random sample from the IDs themselves rather than from the index of the IDs.
Fixing the second problem first
random.subj <- sample(Data$Id, 2)
Actually, if you just want two IDs then that's all you need, but if you want the data for those IDs then
rd <- subset(Data, Data$Id %in% random.subj)
is the correct way to extract it.

Related

R programming: delete rows above and below a specific number from a vector in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a vector in R with zeroes,positive and negative numbers.I want to delete two values above and three values below every positive number and zero using for loops. How can I do it?
Here is a step by step solution:
# Generate a vector of random numbers between -10 and 3
v=round(runif(20)*13-10)
# Get positions of elements >= 0
to_delete=which(v>=0)
# Store position of elemets >=0, positions 1 or 2 before, and positions 1, 2, or 3
# after (i.e. the positions of elements we want to delete)
to_delete=c(to_delete, to_delete-1,to_delete-2,to_delete+1,to_delete+2,to_delete+3)
# Filter positions appearing multiple time and sort them
to_delete=sort(unique(to_delete))
# Keep only positions between 1 and length of initial vector
to_delete=to_delete[which(to_delete>0 & to_delete<=length(v))]
# Delete positions from the initial vector
output=v[-to_delete]
# Print outputs
print(v)
print(to_delete)
print(output)
Note that the output vector will be numeric(0) if there are no elements to keeh

Scale only certain columns R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
How can I scale(x) only certain columns of a dataframe? I have a dataframe with 7 columns and I want to scale only column 3 and 6. The rest should stay as it is.
We can do this with lapply. Subset the columns of interest, loop through them with lapply, assign the output back to the subset of data. Here, we are using c because the outpuf of scale is a matrix with a single column. Using c or as.vector, it gets converted to vector
df[c(3,6)] <- lapply(df[c(3, 6), function(x) c(scale(x)))
Or another option is mutate_at from dplyr
library(dplyr)
df %>%
mutate_at(c(3,6), funs(c(scale(.))))

How R multiples vectors that are not compatible for multiplication [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
When I submit this code to R:
x <- c(1,2,4)
z <- c(7,6,3)
a <- x * z
I get:
a
[1] 7 12 12
So R just multiples element by element. But the two vectors are not compatible for multiplication because the first one has three columns and the second one does not have three rows.
What is happening internally?
Please note that these are vectors; not tables.
This means they can of course be multiplied with each other and would give the expected result through their inner product.

Filtering rows and counting columns - can Reduce or Filter do that? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table like so:
1 2 3 4 5
a b a c
b b
a a a a
c b c b
Is there a special syntax to use for either Filter or Reduce (or something else entirely?) to get it so only the rows with an 'a' (including blanks) are shown? Likewise, is there a built-in way to count the frequency of 'a' for each column or would I have to loop over those individually?
Can't think of a way to pass rows or columns to Reduce or Filter to achieve the first, although a data.frame might get passed in a column-wise fashion for the second question since it is a list of columns. apply is the usual mechanism for doing row-wise operations, but I can think of quicker methods. For the first, under the assumption it is named X and is either a matrix or a data.frame:
X[ rowSums(X=="a", na.rm-TRUE) > 0 , ]
For the second:
colSums( X == "a")

ttest on many columns in Matlab/R [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Does anybody know the examples on how to run paired ttest in Matlab/R/SAS or Python/Java on many columns (I have 1139 variables) in all combinations or selected respective columns in a loop.
thank you
MATLAB Solution:
If I understand correctly, you're just looking for a way to feed ttest with two different columns from your input matrix everytime. You can get all possible combinations of column pairs using nchoosek:
pairs = nchoosek(1:size(X, 2), 2);
Now you can iterate over these indices, each time invoking ttest with a different pair:
for idx = transpose(pairs)
h = ttest(X(:, idx(1)), X(:, idx(2)));
%// Do something with the result...
end

Resources