Why do I get warning longer object length is not a multiple of shorter object length?
You don't give a reproducible example but your warning message tells you exactly what the problem is.
memb
only has a length of 10. I'm guessing the length of dih_y2$MemberID
isn't a multiple of 10. When using ==
, R spits out a warning if it isn't a multiple to let you know that it's probably not doing what you're expecting it to do. ==
does element-wise checking for equality. I suspect what you want to do is find which of the elements of dih_y2$MemberID
are also in the vector memb
. To do this you would want to use the %in%
operator.
dih_col <- which(dih_y2$MemeberID %in% memb)
Longer object length is not a multiple of shorter object length?
Yes, this is something that you should worry about. Check the length of your objects with nrow(). R can auto-replicate objects so that they're the same length if they differ, which means you might be performing operations on mismatched data.
In this case you have an obvious flaw in that your subtracting aggregated data from raw data. These will definitely be of different lengths. I suggest that you merge them as time series (using the dates), then locf(), then do your subtraction. Otherwise merge them by truncating the original dates to the same interval as the aggregated series. Just be very careful that you don't drop observations.
Lastly, as some general advice as you get started: look at the result of your computations to see if they make sense. You might even pull them into a spreadsheet and replicate the results.
Warning: longer object length is not a multiple of shorter object length
The syntax can be rewritten as
Sec <- test * rep(mean(sec), n) + (1 - test) * sec
I believe the problem is that the length of objects is not the same.
Check:
length(test)
n
length(sec)
If n
is one, then you do not need repetition in this case. Try this code:
Sec <- test * mean(sec) + (1 - test) * sec
What are the classes of test
and sec
?
class(test)
class(sec)
What causes a 'longer object length is not a multiple of shorter object length' in this snippet?
dplyr
thinks columnwise not rowwise. That mean it doesn't evaluate n = c(1,2,3,4,5,6)
element after element, but all elements at once.
Doing:
mutate(
data.frame(n = 1:6),
len = length(filter(dice, roll == n)$roll))
I get
n len
1 1 164
2 2 164
3 3 164
4 4 164
5 5 164
6 6 164
Warning message:
In roll == n :
longer object length is not a multiple of shorter object length
Which is the same as:
sum(dice$roll == 1:6)
[1] 164
Warning message:
In dice$roll == 1:6 :
longer object length is not a multiple of shorter object length
Which compares two vectors at their position, recycling the shorter vector as often as necessary, giving a warning when the lenghts don't match.
If you put a rowwise()
in between it evaluates n
element after element:
data.frame(n = 1:6) %>% rowwise() %>% mutate(len = length(filter(dice, roll == n)$roll))
# A tibble: 6 x 2
n len
<int> <int>
1 1 172
2 2 159
3 3 176
4 4 168
5 5 174
6 6 151
What is causing my for/if loop to return 'longer object length is not a multiple of shorter object length' warning in R
"The vectors are taken from loading in an excel file and using 'x <- S1L1$PercentageCoverage' and 'y <- S1L1$FoldCount'."
It looks like you have S1L1 as a dataframe with PercentageCoverage and FoldCount columns and want to create a new column PA when PercentageCoverage >= 1 and FoldCount>=70, then PA should be PercentageCoverage else 0, correct?
S1L1$PA <- ifelse(S1L1$PercentageCoverage>=1 & S1L1$FoldCount>=70, S1L1$PercentageCoverage, 0)
longer object length is not a multiple of shorter object length
When a vector is recycled, it will display a warning message if it has to be "cut off" before it's finished. (as mentioned below, an this is NOT an error message. Error
= R can't complete the function you want it to and so it quits. Warning
= R found something strange about what you're asking it to do but can still do it.*)
For example:
c(1,2) * c(1,2,3,4)
Is equivalent to:
c(1,2,1,2) * c(1,2,3,4)
And displays no warning message. But:
c(1,2) * c(1,2,3,4,5)
Is equivalent to:
c(1,2,1,2,1) * c(1,2,3,4,5)
And displays a warning message, since the last element of the coerced vector is not the last element in the original vector. It will still do the work and give you an answer. The warning is just a warning. See ?warning
.
* See section 2 of this paper
Error longer object length is not a multiple of shorter object length when using difftime
time1
column is of type "POSIXlt"
. I am not really sure why difftime
with units = 'secs'
doesn't work but if you convert it to POSIXct
, it works without any error.
disease_df$time1 <- as.POSIXct(disease_df$time1)
disease_df$diff1 <- as.numeric(difftime(disease_df$time1,
dplyr::lag(disease_df$time1, 1), units = 'secs'))
R Shiny- Output error: longer object length is not a multiple of shorter object length
Perhaps this works
library(shiny)
library(dplyr)
library(DT)
test_2_filtros <- structure(list(var_1 = c("red", "red", "red", "blue", "blue",
"blue", "green", "green", "green"), var_2 = c("table1", "table1",
"table1", "table2", "table2", "table2", "table3", "table3", "table3"
), var_3 = c("column1", "column1", "column1", "column2", "column2",
"column2", "column3", "column3", "column3")), class = "data.frame", row.names = c(NA,
-9L))
df<-test_2_filtros
ui<-(fluidPage(
headerPanel(title = "Shiny App Conditional Filter Demo"),
sidebarLayout(
sidebarPanel(
selectInput("var_1","Select a category",choices = unique(df$var_1), multiple = TRUE, selected= "red"),
selectInput("var_2","Select a table",unique(df$var_2), multiple = TRUE, selected= "table1"),
selectInput("var_3","Select a product",unique(df$var_3), multiple = TRUE, selected= "column1")
),
mainPanel(DT::dataTableOutput("mytable1"))
)
)
)
server<-(function(session,input,output) {
observe({
req(input$var_1)
print(input$var_1)
x <- df$var_2[df$var_1 %in% input$var_1]
updateSelectInput(session,"var_2","Select a table",choices = unique(x), selected= "table1" )
})
observe({
req(input$var_2)
productdata <- df$var_3[df$var_2 %in% input$var_2]
updateSelectInput(session,"var_3","Select a product",choices = unique(productdata), selected= "column1" )
})
result <- reactive({
tmp<-dplyr::filter(df, var_2 %in% unique(input$var_2) & var_3 %in% unique(input$var_3))
tmp%>%
dplyr::mutate(n = "Yes")%>%
mutate(row_num = 1:n()) %>%
tidyr::pivot_wider(names_from = var_3, values_from = n, values_fill = list(n = "No"))%>%
select(-row_num)
})
output$mytable1 <- DT::renderDataTable({
mytable<-DT::datatable(result(), filter= 'top',options = list(order=list(1,'asc'), dom='t', pageLength= 100, autoWidth = TRUE),rownames = FALSE)
formatStyle(mytable, columns = NULL, fontWeight = styleEqual(c('No', 'Yes'), c('normal', 'bold')))
})
})
shinyApp(ui, server)
-output
Related Topics
Painless Way to Install a New Version of R
How to Insert Elements into a Vector
Latitude Longitude Coordinates to State Code in R
How to Uninstall R and Rstudio With All Packages, Settings and Everything Else
Sample N Random Rows Per Group in a Dataframe
What Does the Dot Mean in R - Personal Preference, Naming Convention or More
Creating a Comma Separated Vector
How to Load Packages in R Automatically
Replace All Particular Values in a Data Frame
Forcing Garbage Collection to Run in R With the Gc() Command
Formatting Dates on X Axis in Ggplot2
Create New Dummy Variable Columns from Categorical Variable
How to Center Stacked Percent Barchart Labels