I have the following R script
optioncost =c(5,52,23,15,134,996,2033,18)
options=c(0,1,1,1,1,0,1,1)
cip=c()
for (options_ind in options)
{
if(options_ind==1)
{
cip=append(cip,optioncost[which(options==options_ind)])
}
}
cip
I am trying to get (52 23 15 134 2033 18). Where as when I run the above script I get an output list which is 6 times the length of expected results. My output from the code is as follows for cip " 52 23 15 134 2033 18 52 23 15 134 2033 18 52 23 15 134 2033 18 52 23 15 134 2033 18 52 23 15 134 2033 18 52 23 15 134 2033 18".
Please help me find out where I have gone wrong?
optioncost[which(options==options_ind)] selects the information you want on its own.
The for loop is superfluous and in this case just repeats the process for as many "1"'s as there are in options, which is 6. Which is why your output data is 6 times larger than he data that you want.
optioncost[as.logical(options)]
If you want to work with a for loop then this the way to go
cip=c()
for (i in seq_along(options))
{
if(options[i]==1)
{
cip=append(cip,optioncost[i])
}
}
cip
Related
I have to rename several tens of thousands of audio files of 5 seconds each, each of them coming from a file of 5 minutes (5minutes/5secondes = 60 files). To do this I need to define the time (hour, minute and second) of the beginning of the 5 minutes recording and I tried to make a clock that advances from 5 seconds to 5 seconds and that keep the values of seconds, minutes and hours in vectors to rename the files using these vectors like this:
stwd("")
name = "Car041512-2021-Pass1-Z2_20210914_211000_" #file name prefix
hours = 21
minutes = 19
seconde = 9
for (i in 0:59) {
seconde[i+1] = secondes + i*5
if(seconde[i+1] >= 60)
seconde[i+1] = seconde[i+1] - 60
minute[i+1]= minutes+1
if (minute >= 60)
minutes = 0
hour[i+1] = hours + 1
}
time = as.character(paste0(hour,minute,seconde))
list =list.files(all.files=F)
rename = paste0(name,time,".wav")
file.rename(list, rename)
I have a problem at the beginning of the loop. The seconds vector does not exceed 60 seconds but only during 2 cycles and I do not see why. This is the first time I've done loops with R and I must have made a lot of mistakes.
seconde
[1] 9 14 19 24 29 34 39 44 49 54 59 4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 84 89 94 99 104 109 114 119 124 129 134 139 144 149 154 159 164 169 174 179 184 189 194 199 204 209 214 219 224 229 234 239 244
The renaming of the files works correctly, it's just the loop that doesn't work correctly. Can you help me?
Thanks in advance.
While working on a project, I had a dataframe with a column for which I needed to build a boxplot using highcharter.
The column looked like this:
head(data,20)
Col1
1 30
2 30
3 30
4 30
5 28
6 27
7 29
8 27
9 30
10 30
11 28
12 29
13 29
14 30
15 30
16 30
17 30
18 29
19 30
20 NA
and so on.. data has 693 observations. The data is 'labelled' and hence while building the boxplot, I use as.numeric().
When I try building boxplot with hcboxplot ->
hcboxplot(x = as.numeric(data$Col1[!is.na(data$Col1)]))
The boxplot details I get are -
Maximum: 30
Upper Quartile: 30
Median: 29
Lower Quartile: 28
Minimum: 25
But in data, there definitely are values less than 25 (even 0)
> min(data$Col1[!is.na(data$Col1)])
[1] 0
So, the maximum value is correct but minimum definitely is not.
Why is the boxplot not coming out right ? Any help is appreciated! Apologies for not being able to add a pic to the question (Reputation restrictions).
Thank you.
i need to distribute some days along the year.
I have 213 activities and 247 days.. i need to plan this activities, but i need to cover the maximum time what can be possible.
I am substracting the total days - activities, in this case 34, i divide the total days with the previous result: 247/34= 7,26...
With this number i know what every seven days y have one without activity.
To code this part i doing this:
where day is a "for" variable what its looping a dataframe with dates and integer its the integer part of 7,26, in this case 7
if(day%%integer==0) {
aditional <- 0
} else {
aditional <- 1
}
#
if(day%%7==0) {
aditional <- 0
} else {
aditional <- 1
}
The result will be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
In bold font the day without activity
This way its cool, but its not so precise how i want.
I know i need to use the decimal part of the result of 7,26... 26, but i dont know how do it.
Can you help me please?
Thanks and sorry for my english
Make these 34 days the non-activity days:
round((247/34) * seq(34))
giving:
[1] 7 15 22 29 36 44 51 58 65 73 80 87 94 102 109 116 124 131 138
[20] 145 153 160 167 174 182 189 196 203 211 218 225 232 240 247
This question already has answers here:
Sort (order) data frame rows by multiple columns
(19 answers)
Closed 5 years ago.
I want to order de following sequence code
cereal2[Cereal$Calories & Cereal$Carbs <27.5,]
Name Calories Carbs
1 AppleJacks 117 27
2 Boo Berry 118 27
6 Cocoa Puffs 117 26
7 Cookie Crisp 117 26
8 Corn Flakes 101 24
10 Crispix 113 26
12 Froot Loops 118 26
17 King Vitaman 80 17
18 Kix 87 20
20 Lucky Charms 114 25
21 Multi-Grain Cheerios 108 24
22 Product 19 100 25
25 Rice Chex 94 22
28 Special K 117 22
30 Wheaties 107 24
How can I sort the calories in ascending order?
A simple one liner in base R :)
df <- df[order(df$Calories),]
I am trying to write a for loop which will increment its value by 2. The equivalent code is c is
for (i=0; i<=78; i=i+2)
How do I achieve the same in R?
See ?seq for more info:
for(i in seq(from=1, to=78, by=2)){
# stuff, such as
print(i)
}
or
for(i in seq(1, 78, 2))
p.s. Pardon my C ignorance. There, I just outed myself.
However, this is a way to do what you want in R (please see updated code)
EDIT
After learning a bit of how C works, it looks like the example posted in the question iterates over the following sequence: 0 2 4 6 8 ... 74 76 78.
To replicate that exactly in R, start at 0 instead of at 1, as above.
seq(from=0, to=78, by=2)
[1] 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44
[24] 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78
you can do so in following way, you can put any length upto which you want iteration in place of length(v1), and the increment value at position of 2 to your desired value
for(i in seq(1,length(v1),2))