logo
down
shadow

expand colnames to match the last known one


expand colnames to match the last known one

By : Sai Sandeep
Date : November 22 2020, 04:01 AM
it helps some times Assuming you want to rename columns that start with "X", we could replace them with NA and then use na.locf to get previous names of the column.
code :
library(zoo)

names(df) <- na.locf(replace(names(df), grepl("^X", names(df)), NA))
df
#       Dates Var1 Var1 Var1 Var2 Var2 Var2
#1 2017-05-02    1    3    5    7    9   11
#2 2017-07-30    2    4    6    8   10   12


Share : facebook icon twitter icon
match two data.frames according to colnames

match two data.frames according to colnames


By : mat_hager
Date : March 29 2020, 07:55 AM
should help you out I have two large data.frames: , (Upgraded from a comment.)
It sounds like
code :
sapply(names(DF1),function(n) nrow(merge(DF1,DF2,by=n))
Error in colnames <-`(`*tmp*`... attempt to set 'colnames' on an object with less than two dimensions

Error in colnames <-`(`*tmp*`... attempt to set 'colnames' on an object with less than two dimensions


By : S. Carter
Date : March 29 2020, 07:55 AM
To fix this issue I am using the function predict to predict for new variables. When I feed the new variables (as a data frame with column names) to the predict function, I get the following error: , I've got the same error while using the DESeq R package:
R rowsums if colnames match two arguments in a second attribute table

R rowsums if colnames match two arguments in a second attribute table


By : Dmedin
Date : March 29 2020, 07:55 AM
help you fix your problem I want to calculate rowsums only if colnames (i.e. species) of my data frame match two arguments in a second attribute table. This means it shoul first match the name in a column of the attributes table AND have a certain entry in another column of the attribute table. However, the attribute table contains more species than the orginal data frame. , This is easy to solve if you convert the dataset into a long format
code :
library(dplyr)
library(tidyr)
data(varespec, package = "vegan")
attributes <- data.frame(
  Species = c(colnames(varespec), "spec1", "spec2"),
  Attribute = c(rep(c("MI", "PI"), c(14, 30)), "MI", "PI")
)
varespec %>% 
  add_rownames("ID") %>% 
  gather(Species, Value, -ID) %>% #convert to long format
  inner_join(attributes, by = "Species") %>% 
  filter(Attribute == "MI") %>% 
  group_by(ID) %>% 
  summarise(Total = sum(Value))
dividing all values in one column by values in a separate data frame (colnames match)

dividing all values in one column by values in a separate data frame (colnames match)


By : Sticks Mabakane
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I need to divide every value in a given column of the first data frame by the value in corresponding column name of the second data frame. For example, I need to divide every value in the 0 column of demand_copy by 25.5, every value in the 1 column by 13.0, etc. and get an output that is the same structure as the first data frame , You could use
code :
t(t(demand_copy) / medians[, 1])
sweep(demand_copy, 2, medians[, 1], "/")
R changes type of colnames from Date to numeric while assigning new colnames

R changes type of colnames from Date to numeric while assigning new colnames


By : Marcell Németh
Date : March 29 2020, 07:55 AM
Hope that helps I would like to keep colnames in date format, but R changes it automatically into numeric and after that into character string. , @Sotos comment is a solution:
code :
colnames(db) <- as.character(c(seq(as.Date("2013-01-01"), length.out = 60, by= 'days')))
Related Posts Related Posts :
  • Why isn't string splitting after last open parenthesis?
  • blogdown - how do I specify which page a post will appear on
  • Can I span groups of categories with horizontal lines in ggplot2
  • RODBC gives proper row count but yields empty query
  • Merging two incomplete factors
  • reshaping data with time represented as spells
  • R ggplot: How to create a scatter plot with marginal box plots
  • Rcpp use outer with pmax
  • Symbolic matrix mutiplication error (Ryacas)
  • Creating columns based on total number of columns in a data frame R
  • How do I address R raster mosaic error: 'data' must be of a vector type, was 'NULL'?
  • ggplot2() plotting one variable against itself by factor?
  • Random sets with three random numbers in it (sampling random points in a cube)
  • Function to find varying strings
  • How do I post some introductory paragraphs on the main page of my blogdown site?
  • Correlations between numerous variables grouped in dplyr
  • Animating 3d object in R Markdown html with play3d
  • Suppress multiple package/library loading messages
  • R CMD REMOVE has no effect
  • repeated observations average per month
  • Count values per year and based on other column
  • aggregating elements to create groups of minimal size
  • efficient subsetting of data.table with greater-than, less-than using indices
  • Prefix/suffix column content with column names
  • ggplot and ggsignif error on grouping variable
  • R - How to use sum and group_by inside apply?
  • For Each Loop to convert into numeric values
  • How to summarise taking a random value from a categorical column?
  • Predictions in SageMaker ::: Writing Function To Split Big Data-frame Into Batches For Predictions
  • ggplot: How to keep marker colours in legend but hide text colours?
  • Removing character elements from a vector
  • Cumulative sum based on factor on R
  • `testthat::expect_silent()` does not seem to notice ggplot2 errors
  • Parse Factor in R
  • Exit from Command prompt after running r script
  • R: write function with optional arguments
  • After full_join() how to replace NAs in one source with data from other source
  • Use reactive "if" statements in R Shiny
  • Nested loop to find most recent comment
  • How to calculate common values across different groups?
  • RStudio + Knitr + mind mapping
  • Filter a dataframe between two dates
  • Filter rows with earlier date than a row specified by another variable
  • generate a vector between 0 and 1, with certain length and certain number of 1
  • Looping multiple listed data frames into a single function
  • Problems importing .csv file due to presence of \ symbol
  • R: filter/subset range of rows based on cells containing specific value
  • merging by common value in R
  • R - Conditionally select within duplicate IDs and index
  • Generate sequences of anniversary dates between 2 dates
  • Have trouble creating a bar graph with ggplot with having both x and y variables
  • Function to remove near close proximity geolocations geometry in R
  • How to add legend and table with data value into a chart with different lines using ggplot2
  • Adding non-numeric values to a histogram in SAS or R
  • Add variable labels within mutate
  • Collapse matrix to vector and replace values with column names
  • Merge returning matrix of size x^2
  • Fuzzy compare and aggregate similar records within a single single column data-frame
  • Collapsing consecutive dates into a single row
  • How to change default color scheme in ggplot2?
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org