How to save a data frame as CSV to a user selected location using tcltk
Take a look at the write.csv
or the write.table
functions. You just have to supply the file name the user selects to the file
parameter, and the dataframe to the x
parameter:
write.csv(x=df, file="myFileName")
How can I export in R
Native R: write.csv(Data_passed_slim, "nn.csv")
.
See ?write.csv
for documentation.
But, for larger datasets I would recommend using data.table::fwrite()
. Documentation: https://www.rdocumentation.org/packages/data.table/versions/1.14.0/topics/fwrite
Mean values from multiple csv to data frame
I'd also suggest to use (l)apply... Here's my take:
getMeans <- function(fpath,runfct,
target_cols = c(2),
sep=",",
dec=".",
header = T,
min_obs_threshold = 3){
f <- list.files(fpath)
fcsv <- f[grepl("\.csv",f)]
fcsv <- paste0(fpath,fcsv)
csv_list <- lapply(fcsv,read.table,sep = sep,
dec = dec, header = header)
csv_rows <- sapply(csv_list,nrow)
rel_csv_list <- csv_list[!(csv_rows < min_obs_threshold)]
lapply(rel_csv_list,function(x) colMeans(x[,target_cols]))
}
Also with that kind of error message, the debugger might be very helpful.
Just run debug(moist.each.mean)
and execute the function stepwise.
Split one cell data into two variables before save in csv
Yes. Use tidy
's seperate
to split a column into segments of an original one:
library(tidyverse)
df <- tibble(
`Observation time` = c(
'141102/0000',
'141103/0000',
'141104/0000'
)
)
df %>% separate(
col = `Observation time`,
into = c('Date', 'Time'),
sep = '/'
)
This outputs:
# A tibble: 3 x 2
Date Time
<chr> <chr>
1 141102 0000
2 141103 0000
3 141104 0000
Note that the Observation time
column is dropped and replaced by Date
and Time
. If you want, you can convert them back to integer (or something else) via a mutate
.
How to import output from R to an Excel File
@iceiceice you could try this
stock_reco<- read.csv("C:\\temp\\Migration.csv")
migrate<-as.matrix(stock_reco)
title<-colnames(migrate)
dt1<-migrate[,3]
dt2<-as.Date(dt1, format= "%d/%m/%Y")
reco1<-migrate[,6]
reco<-as.matrix(reco1)
for (i in 1:4099) {
if((migrate[i,1]== migrate[i+1,1]) && (migrate[i,2]== migrate[i+1,2]))
{
k<-difftime(dt2[i+1],dt2[i],units = "days")
if((k <=180) && (reco[i] == reco[i+1]))
d <- rbind(d, data.frame(migrate[i,], migrate[i+1,]))
}
}
write.csv(file=fileName, x='d')
Read csv using gfilebrowse from gWidgets
Here is the script after the improvements. Now it does exactly what I expect it to do. I hope someone finds it useful:
# load functions ####
# download function
f.d <- function(hdf.urls,hdf.names,out.dir){
for(i in 1:length(hdf.urls)){
URL <- hdf.urls [i]
file <- hdf.names [i]
download.file(URL,paste(out.dir,"/",file,sep=""),mode="wb")
}}
# read csv function
f.csv <- function(x){
df1 <<- read.csv(x,header=TRUE,sep=",")
hdf.urls <<- df1$Online.Access.URLs
hdf.urls <<- as.character(hdf.urls)
hdf.names <<- df1$Producer.Granule.ID
hdf.names <<- as.character(hdf.names)
}
# load functions ####
# my.DownloadHDFv2 this one works fine ####
my.DownloadHDF <- function(){
options(guiToolkit = "tcltk")
win <- gwindow("Download HDF with R!", visible = FALSE)
csv.frame <- gframe("csv with HDFs names ", container = win)
a <- gfilebrowse("Upload csv file",cont=csv.frame,
handler=function(h,...){
f.csv(svalue(a))
})
path.frame <- gframe("Output Directory ", container = win)
brow <- gfilebrowse(text = "Select folder...", type = "selectdir",container=path.frame,
handler=function(h,...){
out.dir <<- svalue(brow)
})
b <- gbutton(text="Start Download",container = win,
handler = function(h,...){
f.d(hdf.urls,hdf.names,out.dir=out.dir)
})
visible(win)<-TRUE
}
my.DownloadHDF()
# my.DownloadHDFv2 this one works fine ####
Related Topics
R Ggplot2 Add Today's Date to the Title
Running Multiple Linear Regressions Across Several Columns of a Data Frame in R
Scale_Color_Manual Colors Won't Change
Trying to Find Row Associated with Max Value in Dataframe R
Difference Between Mean(C(1,2,21)) and Mean(1,2,21)
Lookup Values Corresponding to the Closest Date
Convert String Back into Object in R
Multiple Graphs of Each Time Series
Subset Dataframe Such That All Values in Each Row Are Less Than a Certain Value
R: Arranging Multiple Plots Together Using Gridextra
What Are Some Good Books, Web Resources, and Projects for Learning R
Move a Column to First Position in a Data Frame
Delete Rows with Blank Values in One Particular Column
How to Plot the Results of a Mixed Model
To Find Whether a Column Exists in Data Frame or Not