at the end of my wits, so sorry this is the wrong place, or done incorrectly. first time asking here. i am new to R, with very little programming experience (a pascal class in college, and was very good at macromedia lingo way back - so, not that afraid of code).
to keep things short and simple, i think best to just show you what i have, and what i would like. i have spent hours upon hours searching and trying for a solution.
an example of what i have (it is an xts object called "signals", and indexed by days (left out here to make the example simple):
open close position
0 0 0
1 0 0
0 0 0
0 0 0
0 1 0
0 0 0
and what i would like to happen:
open close position
0 0 0
1 0 1
0 0 1
0 0 1
0 1 1
0 0 0
basically, when "open" is true, repeat 1s in "position" until "close" is true. amazingly simple, i think, but somehow i can't make it work. here one example of where i got that i thought was maybe close, but it gets stuck in an endless loop:
for (i in 1:nrow(signals)) {
if (signals[i,"open"]==1) next
while (signals[i,"close"] == 0) {
signals[i,"position"] <- 1 }
}
thank you!
EDIT - i left out an important qualifier. there are times where the first true statement in "close" will come before the first true statement in "open." however, now that i wrote that out here, i suppose it is easier to just 'clean' the close column somehow, so there are no 1s in it prior to the point of the first 1 in the open column.
however, if someone has an idea how to do it all, feel free to add additional information. thanks!
You don't have to use loops for this:
open <- c(0,1,0,0,0,0)
close <- c(0,0,0,0,1,0)
position <- cumsum(open-close)
position
[1] 0 1 1 1 0 0
Note this closes immediately, if you want to on the line after you get a close signal, use:
cumsum(open-c(0,close[-length(close)]))
[1] 0 1 1 1 1 0
The reason your while statment never ends is that you have nothing to modify what is being tested, that is i doesn't get incremented.
Related
I'm really new to this, so please forgive my general lack of understanding.
I've been trying to take the dataset that is provided here (I download the .txt file at the bottom). It's scRNA-seq data and I'm trying to use seurat to process it and make some graphs. However, every time I try loading it, it doesn't work and it says I need a arcode file. How can I convert this file into something that'll run through seurat and make a barcode file? I really appreciate anyone's help in this. Thank you!
Reading it is trivial, use standard functions such as:
d <- data.table::fread("GSM4203181_data.raw.matrix.txt")
The "barcodes" are the colnames, the "features" are the first column, so the genes.
> d[1:5,1:5]
V1 AAACCTGAGAGATGAG-1 AAACCTGCACCAGGCT-1 AAACCTGGTTAAGACA-1
1: RP11-34P13.7 0 0 0
2: RP11-34P13.8 0 0 0
3: FO538757.2 1 0 0
4: AP006222.2 2 1 0
5: RP4-669L17.10 0 0 0
AAACCTGGTTGAGTTC-1
1: 0
2: 0
3: 0
4: 0
5: 0
From here you can construct your Seurat object manually, e.g. via https://www.rdocumentation.org/packages/Seurat/versions/3.0.1/topics/CreateSeuratObject
I am running a logistic regression and I want to control for the country of the respondents. I have 12 countries. I used the "fastDummy" package to create dummies for each country
ALL<-dummy_cols(ALL, select_columns = "country")
I get something like this:
country_Japan 1 1 0 0 0 0
country_Taiwan 0 0 1 1 0 0
country_China 0 0 0 0 1 1
and so on...
As you can see, the sum of all variables makes a perfect collinearity. For this reason, I cannot estimate the model.
I read that I need to include a variable with 0s as the last country dummy to avoid this collinearity. Is this correct? I included the intercept (a column with 1s) , but it did not help.
I would appreciate your suggestions. Thanks
Check the remove_first_dummy parameter in the dummy_cols function, i.e. set it to TRUE. This should solve your problem of multicollinearity.
I have a binary time series with 359 observations.
like this; 0
0
0
0
0
0
0
0
0
1
0
0
0
0
1
0
0
0
...
I want to generate n data samples with same intervals but permuted order.
For this at first I found times which original data became one from zero something like this:
147 65 10 251
and then randomized the order of intervals into something like these:
251 10 65 147
10 251 147 65
.
.
.
and so far my code is something like this:
mydata <- "C:/Users/me/Desktop/2.xlsx"
library("xlsx")
library("tseries")
my_data <- read.xlsx(mydata, sheetName = "Sheet1", header = F)
file <- "C:/Users/me/Desktop/pp.xlsx"
ts=my_data[6]
ts=unlist(ts)
for (i in 1:100){
diff.ts<-diff(ts)
x=sample(diff(which(diff.ts==1)))
print(x)
write.xlsx(x,file[i], sheetName = "Sheet1",col.names=TRUE, row.names=FALSE, append=FALSE, password=NULL)
}
however,
I can not store all of these in .xlsx file even though while printing they seem fine
my second problem is that I do not only want to know which time 0 to one happened but also I want to write them as the original data for example if in one of the randomized samples the intervals is 10 251 147 65 I want a stored column with 1 in the 10th, 251th, 147th and 65th row as one and the other row as zero something like this :
0 0 0 0 0 0 0 0 0 1 0 0 .... .
Sorry for English errors
The interval objective is unclear and your permutated interval question is perhaps answered with the sample() function below, which will randomly pick a distribution of 1s and 0s. You can also adjust the probabilities to change change a 0 or 1 is selected, here it's 50/50%. Additionally, if you want a random sample but to ensure your code is repeatable, you can enforce a random seed in your session to draw the same permutation each time with: set.seed(123456), picking whatever seed you feel appropriate.
sample(x=c(0,1),size=359,replace=T,prob=c(0.5,0.5))
Alternatively, your question might suggest wanting to set values equal to 1 at a specific index. Here, for your example of 147,65,10,251 you can do:
intervals <- rep(0,359)
intervals[c(147,65,10,251)] <- 1
Or perhaps like this?
intervals <- rep(0,359)
intervals[sample(c(147,65,10,251))] <- 1
As this is a two part question and an answer to your excel writing issue: you have the write.xlsx called from within the for loop, meaning that you're writing the file at each loop. This may or may not be the behavior you want? I assume writing the entire dataset is preferable. However, as you're specifying file[i] as the output, and your variable file is a single value (or one-length vector) you'll get errors. You change this in the write.xlsx to write.xlsx(x,paste0("my_file_num",i,".xlsx"), ... or move the call outside the loop as illustrated below
file <- "C:/Users/me/Desktop/pp.xlsx"
ts=my_data[6]
ts=unlist(ts)
samples <- NULL
for (i in 1:100){
diff.ts<-diff(ts)
x=sample(diff(which(diff.ts==1)))
samples <- append(samples,list(samples=x))
print(x)
}
write.xlsx(samples,file, sheetName = "Sheet1",col.names=TRUE, row.names=FALSE, append=FALSE, password=NULL)
I am performing some prediction models. I have 2 binary columns , one with predicted values and the other one with the actual values.
Since the columns have few ones because it counts the number of people with cancer, i want to observe how many cases the model detected(how many real ones it predicted) and the percentage of sick persons correctly predicted.
Brief description of the data: the first column shows the real values and the seconde one shows the predicted values:
> predictedvsreal
real prediction
39240 0 0
39241 0 0
39242 0 0
39243 1 0
39244 0 1
39245 0 0
39246 0 0
39247 0 0
39248 1 1
39249 0 0
39250 0 0
39251 0 0
39252 0 0
Thanks!
Next time please include a reproducible example as it makes the question much better - both for letting people who answer have a concrete example to work with and to catch edge-cases, and for future readers to see a real example.
There are lots of good recommendations for how to create nice, minimal, reproducible examples at this link.
From what you describe, you want the table function, probably like this:
with(your_data, table(your_first_column_name, your_second_column_name))
i am stuck with a boolean expression help me solve what x.y'+x'.y =?
i have exam today and i don't know how do solve this type. And in addition can someone recreate the boolean laws that involve two element instead of one for me? Thank you
There are only two inputs to the expression, so write out a truth table with the values of the inputs and for each term until you get the result.
x y x' y' x'.y x.y' x'.y+x.y'
0 0 1 1 0 ...
0 1 1 0 1 ...
1 1 0 0 0 ...
1 0 0 1 0 ...
When you have done that, look for patterns in the last column. You should then recognise the pattern as being the same as a single operator.
The pattern for the inputs is usually a Gray code so that the output column reflects changes due to only one input changing, which usually can help show up the pattern.
Alternatively, when you have your result, plot it in a grid and spot the pattern that way, e.g. for x+y you'd get
x\y 0 1
0 0 1
0 1 1