Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I format dates in R? I had to change something in my data set to account for blanks, and now my dates are very large negative numbers. I need to change them back into dates.
Assuming you have \t delimited input file.
Use the as.is argument to stop the read.table() function from converting the input variables. Then perhaps convert the date into something usable using strptime()
data <- read.table(file="...", sep="\t", as.is = TRUE)
data[,1] <- strptime(data[,1], "%Y-%m-%d")
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm quite new to R and currently stuck on the following task. I have spatial data in the following format:
lat long
1 49,6837508756316 8,97846155698244
2 49,9917393661473 8,2382869720459
3 51,308416699361 12,4118696787101
4 50,7048668720388 6,62725165486336
...
and so on. It's a pretty large data set.
I've been advised to convert my data set into sf data to properly work with it. Can somebody help my with that? I think one problem might also be, that the decimal mark is an ,.
Thanks for your help guys!
I assume the data is in a data.frame called sf:
sf <- data.frame(lat=c("49,6837508756316","49,9917393661473","51,308416699361","50,7048668720388"),long=c("8,97846155698244","8,2382869720459","12,4118696787101","6,62725165486336"), stringsAsFactors = FALSE)
The problem is, that the entries are characters, so you have to convert them to numeric. This can be done via as.numeric, but this function expects the decimals to be seperated by a dot ., hence you have to convert the comma to a dot and then call as.numeric. The conversion can be done using the function gsub.
sf$lat <- as.numeric(gsub(",",".",sf$lat))
sf$long <- as.numeric(gsub(",",".",sf$long))
If you have many columns and you dont want to copy-paste the above for every column, I would suggest you to use:
sf[] <- lapply(sf, function(colValues) as.numeric(gsub(",",".",colValues)))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to convert factors from a data-frame to numeric using the commands
data[] <- lapply (data, function(x) as.numeric(as.character(x))
But it keeps asking me for more coding. What am I doing wrong?
The data-frame is named data and it consists of 50 rows and 2 columns. Will this command change every variable in numeric right? Or shall I do something else?
screenshot after using 'dput' at http://imgur.com/Sde9QSk.png
Shouldn't you add ) at the end of your code?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I know this has been asked here, but i did according to several answers but still received NA value, or value changed my year 2016 into 2020.
I need to convert factor into date. I tried as.Date function
Reporting_Raw_Data$Click_Date <- as.Date(Reporting_Raw_Data$Click_Date,"%d%m%y")
also tried:
Reporting_Raw_Data$Submit_Date <- as.Date(Reporting_Raw_Data$Submit_Date,format="%d-%m-%Y")
also:
Reporting_Raw_Data$Approve_Date <- as.Date(Reporting_Raw_Data$Approve_Date, format="%d-%m-%y")
I also tried POSIXct:
Reporting_Raw_Data$Click_Date <- format(as.POSIXct(strptime(Reporting_Raw_Data$Click_Date,
"%Y-%M-%D %H:%M:%S",tz="")),
format = "%Y-%M-%D")
also tried ymd_hms
Reporting_Raw_Data$Click_Date <- ymd_hms(Reporting_Raw_Data$Click_Date)
None gave me correct conversion.
sample table looks like below:
Click_Date
8/16/2016
8/7/2016
Maybe this could work:
Click_Date <- factor(c("8/16/2016",
"8/7/2016"))
strptime(as.character(Click_Date), "%m/%e/%Y")
This works fine for me:
as.POSIXct(x, format="%m/%d/%Y")
This is a data format knowledge issue.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Currently I have a column with time in the format yyyy-mm-dd hh:mm:ss, (eg. 2015-10-10 04:10:45) and I wish to extract the hour possibly using as.POSIXlt(x)$hour where x is my column.
Unfortunately, this function is returning a vector full of 0's, but if I do something like as.POSIXlt("2015-10-10 04:10:45")$hour I receive 4 which is what I want.
How can I do this with the whole column?
I was just doing the exact same thing on my dataset...
format(as.POSIXct(df$datetime, format="%Y-%m-%d %H:%M:%S"), format="%H:%M:%S")
#[1] "04:10:45"
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a data frame that has some empty entries. I set the
options(stringsAsFactors = FALSE)
so that I can change the empty cells. I then wrote the following code:
apply(my_data[,6:65],2, function(x) x[which(x=='')]<-0)
, hoping that it replaces all the empty cells with zeros. But it isn't working!
Note that my_data has 65 columns and columns 1:5 contain string.
Thanks in advance
No need to use apply, just use [<- with logical indexing
my_data[my_data==""] <- 0