r

dplyr Update a cell in a data.frame

风流意气都作罢 提交于 2021-02-20 18:51:46
问题 df <-data.frame(x=c(1:5),y=c(letters[1:5])) Let's say I want to modify the last row, update.row<-filter(df,x==5) %>% mutate(y="R") How do I update this row into the data.frame ? The only way, I found albeit a strange way is to do an anti-join and append the results. df <-anti_join(df,update.row,by="x") %>% bind_rows(update.row) However, it seems like a very inelegant way to achieve a simple task. Any ideas are much appreciated... 回答1: If you are insistant on dplyr , perhaps df <-data.frame(x

R Shiny Dashboard does not load rendered UI inside of sidebarMenu on initialization

痞子三分冷 提交于 2021-02-20 18:47:56
问题 I'm trying to generate a UI element that is dynamic based on how the user wishes to provide their input. I'm using the Shiny Dashboard for simplicity but I have encountered an issue with the sidebarMenu. Previously when I was placing static UI elements directly into the sidebarMenu, I had no issue, however when trying to place dynamic UI elements into the sidebarMenu I have problems. I'm using R 3.3.2 and Shiny 1.0.0 and Dashboard 0.5.3. The specific problem I have is that when the program

R Shiny Dashboard does not load rendered UI inside of sidebarMenu on initialization

允我心安 提交于 2021-02-20 18:43:36
问题 I'm trying to generate a UI element that is dynamic based on how the user wishes to provide their input. I'm using the Shiny Dashboard for simplicity but I have encountered an issue with the sidebarMenu. Previously when I was placing static UI elements directly into the sidebarMenu, I had no issue, however when trying to place dynamic UI elements into the sidebarMenu I have problems. I'm using R 3.3.2 and Shiny 1.0.0 and Dashboard 0.5.3. The specific problem I have is that when the program

Take difference between first and last observations in a row, where each row is different

倾然丶 夕夏残阳落幕 提交于 2021-02-20 17:56:41
问题 I have data that looks like the following: Region X2012 X2013 X2014 X2015 X2016 X2017 1 1 10 11 12 13 14 15 2 2 NA 17 14 NA 23 NA 3 3 12 18 18 NA 23 NA 4 4 NA NA 15 28 NA 38 5 5 14 18.5 16 27 25 39 6 6 15 NA 17 27.5 NA 39 The numbers are irrelevant here but what I am trying to do is take the difference between the earliest and latest observed points in each row to make a new column for the difference where: Region Diff 1 (15 - 10) = 5 2 (23 - 17) = 6 and so on, not actually showing the

Length of longest stretch of NAs in a column of data-frame object

血红的双手。 提交于 2021-02-20 16:18:41
问题 I want to write a code that finds the length of longest continuous stretch of NA values in a column of a data-frame object. >> df [,1] [,2] [1,] 1 1 [2,] NA 1 [3,] 2 4 [4,] NA NA [6,] 1 NA [7,] NA 8 [8,] NA NA [9,] NA 6 # e.g. >> longestNAstrech(df[,1]) >> 3 >> longestNAstrech(df[,2]) >> 2 # What should be the length of longestNAstrech()? 回答1: Using base R we could create a function longestNAstrech <- function(x) { with(rle(is.na(x)), max(lengths[values])) } longestNAstrech(df[, 1]) #[1] 3

Length of longest stretch of NAs in a column of data-frame object

梦想的初衷 提交于 2021-02-20 16:17:28
问题 I want to write a code that finds the length of longest continuous stretch of NA values in a column of a data-frame object. >> df [,1] [,2] [1,] 1 1 [2,] NA 1 [3,] 2 4 [4,] NA NA [6,] 1 NA [7,] NA 8 [8,] NA NA [9,] NA 6 # e.g. >> longestNAstrech(df[,1]) >> 3 >> longestNAstrech(df[,2]) >> 2 # What should be the length of longestNAstrech()? 回答1: Using base R we could create a function longestNAstrech <- function(x) { with(rle(is.na(x)), max(lengths[values])) } longestNAstrech(df[, 1]) #[1] 3

Save knitr::kable() output to html file R

隐身守侯 提交于 2021-02-20 16:15:12
问题 I have a knitr_kable output that I want to save as an HTML document from R. I need this to run automatically from my R script with no human involvement. For example: dt <- mtcars[1:5, 1:6] kable(dt, "html") %>% kable_styling(bootstrap_options = c("striped", "hover")) This has html output but the class is knitr_kable so I can't write it to a table or html file because it cannot be coerced to a dataframe. class(kable(dt, "html")) [1] "knitr_kable" Does anyone have a method for saving one of

how do you convert output from readLines to data frame in R

情到浓时终转凉″ 提交于 2021-02-20 16:03:27
问题 I have a txt file that has multiple lines. Each line as text that is separated by space. Number of columns in each line may be different. I need to read each line one at a time, put it into data frame and print it. I tried this: x<-readLines("output.txt") for (i in 2:length(x) ) { data<-data.frame(x[[i]]) print(data) } I have to start from line number 2 becasuse line number 1 has some heading info that I dont need. For example, this prints out something like this: x[[2]] [1] " dcserver AIX

Read QRcode from scanned document in R

别说谁变了你拦得住时间么 提交于 2021-02-20 15:34:48
问题 I am using the qrcode_gen() function from the qrcode package in R to generate a QR code, then place it on the top of a document (example linked below). The document is printed, written on, scanned, and stored as an image (.png) file. I would like to be able to read the QR code in R to get the imbedded text back. Eventually the whole process will be semi-automated in an Rshiny app, so I am hoping to keep everything in R , and not need to use an outside web tool to read the code. Has anyone

Read QRcode from scanned document in R

可紊 提交于 2021-02-20 15:31:41
问题 I am using the qrcode_gen() function from the qrcode package in R to generate a QR code, then place it on the top of a document (example linked below). The document is printed, written on, scanned, and stored as an image (.png) file. I would like to be able to read the QR code in R to get the imbedded text back. Eventually the whole process will be semi-automated in an Rshiny app, so I am hoping to keep everything in R , and not need to use an outside web tool to read the code. Has anyone