I have a data frame (size:36 rows, 2000 columns), which in the last row it contains dates for each column in this format "YYYY-MM-DD".
How can I sort the columns using the dates in the last row?
My attempts so far:
df[order(as.Date(df["Dates",], format="%Y-%m-%d")),]
df[order(lubridate::ymd(df["Dates",])),]
Thanks
We can extract the last row and do the order
df1[order(as.Date(unlist(tail(df1, 1))))]
Related
I have a dataframe that looks like this
I have three columns of interest, prey.kill, livestock.depredation and ete.hour
These columns are duplicated 50 times in the wide format e.g. prey.kill.1, prey.kill.2 etc
What I want to do is have a single column for each of these with 50 rows in long format.
How do I almagamate all the data and then translate it to a long format in R?
Thanks
I have a data frame with many columns and rows. I wish to find the number of rows in two columns that both give '1' as simply as possible.
I have a problem where the dates are in column 1 of my first dataframe. I want them in the column with the row numbers like my second dataframe. Below is the dput code.
The two dput codes below show two different date settings. One of the dataframe settings show the dates that I have in column1 and the second dataframe has setting which what I want. The date column is in with the number columns and there is no title column for date.
I think you want to convert the dataframe into xts object. If the first dataframe is called df1 you can convert the Date column to date class and do :
df2 <- xts::xts(df1[-1], as.Date(df1$Date))
df2
# CC1G HYLA IHHG
#2020-10-27 21328 545.65 473.52
#2020-10-28 20915 539.00 469.95
#2020-10-29 21050 538.10 469.55
#2020-10-30 20723 538.15 470.90
#2020-11-02 21305 539.00 471.82
#2020-11-03 21430 544.35 474.60
#2020-11-04 21938 549.40 480.95
#2020-11-05 22048 555.50 482.80
#2020-11-06 22043 555.15 481.97
#2020-11-09 22693 562.50 486.05
#2020-11-10 22555 557.10 483.95
#2020-11-11 22928 556.45 483.67
#2020-11-12 22860 554.90 470.77
#2020-11-13 22360 555.60 470.80
I have a very large data frame with Timestamp, StationId and Value as column names.
I would like to create a new matrix where the rows are Timestamps, columns are StationIds and the matrix elements are Values.
I have tried doing so using a loop but it is taking very long:
for (row in 1:nrow(res))
{
rmatrix[toString(res[row,"Timestamp"]),toString(res[row,"StationId"])] <-
res[row,"Value"]
}
The 'res' data frame looks like this. The timestamps are for a year, at 5mins interval. There are 62 unique station ids. The elements in the Value column are actually rainfall values.
The rmatrix I'm trying to rearrange the data into looks like this. Each row is a unique timestamp at 5mins interval. Each column is the id of a station. The elements of the matrix are supposed to be the rainfall value for that station at that time.
Is there a faster way to do this?
library(tidyverse)
df <- res %>% spread(StationIds,Values)
I have a data frame of 1530 columns, the first 800 of which I want to subset to have a common name and the last 730 of which I want to subset to have a common name. My attempts to rename the first subset have been colnames(dataframe)[1:800] <- c("NonNorm") and colnames(dataframe)[1:800] <- rep("NonNorm", 800).
However, when I view the altered data frame, all of the values in the rows of the first column have been duplicated across all of the columns through 800. Why is this happening and how do I preserve the original values while changing the column names?