Sequentially Rename 100+ Columns Having Idiosyncratic Names

renaming multiple columns of vectors in data frame without typing all names

To demonstrate MrFlick's solution in action.

df <- as.data.frame(matrix(1:20, ncol = 10))

# Without column names
df
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> 1 1 3 5 7 9 11 13 15 17 19
#> 2 2 4 6 8 10 12 14 16 18 20

# Add column names quickly
colnames(df) <- paste0("Col", 1:10)

# With column names
df
#> Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Col9 Col10
#> 1 1 3 5 7 9 11 13 15 17 19
#> 2 2 4 6 8 10 12 14 16 18 20

Created on 2020-09-25 by the reprex package (v0.3.0)

Renaming columns automatically after using ldply in R

This should do it

colnames(mydf1) <- paste0("MYDF_", seq_len(ncol(mydf1))) 

Updated based on Konrad Rudolph suggestion.

R rename columns (hlookup)

using dplyr 's rename_at and passing an anonymous functions seems to fit your purposes

require(dplyr)

data <- data.frame(c=1:5, ID=0,a=2,b=5:9)

data
# c ID a b
# 1 1 0 2 5
# 2 2 0 2 6
# 3 3 0 2 7
# 4 4 0 2 8
# 5 5 0 2 9

cols_iwant_to_rename <- c("a","b","c")

data <-
data %>% rename_at(cols_iwant_to_rename , function(x) paste0(x, x))

data

# cc ID aa bb
# 1 1 0 2 5
# 2 2 0 2 6
# 3 3 0 2 7
# 4 4 0 2 8
# 5 5 0 2 9

quantile regression graph (grqreg) interprets titles with multiple words as associated with multiple graphs

grqreg is a user-written command from SSC and must be installed before your code can be attempted. On Statalist you'd be expected to explain that and there's no reason for lower standards here.

You're correct that the title() option is programmed to peel off one word for each graph used, but although that is idiosyncratic, the option is documented in the help for grqreg as working in that way. Trying to force a multiple word title even with " " to bind won't work as the option is declared title(string) which means that the string delimiters are stripped on input. You could try to subvert that by hacking at the code to insist on title(string asis). However, that doesn't seem worth the bother as there is a much easier work-around.

The option t1title() is available to you. If getting the style and/or position the same as title() would do normally is important to you, then add suboptions as needed.

* setup
webuse auto, clear
keep price mpg headroom foreign
compress

* running quantile regression
qreg price mpg headroom foreign

* creating a quantile regression plot for the binary variable foreign
* must install previously with -ssc inst grqreg-
grqreg foreign, ci ols olsci graphregion(color(white))

* so far everything works and is uncontroversial
* now I quietly re-run the quantile regression
quietly: qreg price mpg headroom foreign
*and try to put a title on this graph with multiple words

* t1title() is a work-around for title()
* grqreg foreign, ci ols olsci t1title(this is a title using multiple words)
grqreg foreign, ci ols olsci t1title(this is a title using multiple words, size(large) )

How to group timestamps into islands (based on arbitrary gap)?

SELECT done, count(*) FILTER (WHERE step) OVER (ORDER BY done) AS grp
FROM (
SELECT done
, lag(done) OVER (ORDER BY done) <= done - interval '2 min' AS step
FROM tbl
) sub
ORDER BY done;

The subquery sub returns step = true if the previous row is at least 2 min away - sorted by the timestamp column done itself in this case.

The outer query adds a rolling count of steps, effectively the group number (grp) - combining the aggregate FILTER clause with another window function.

fiddle

Related:

  • Query to find all timestamps more than a certain interval apart
  • How to label groups in postgresql when group belonging depends on the preceding line?
  • Select longest continuous sequence
  • Grouping or Window

About the aggregate FILTER clause:

  • Aggregate columns with additional (distinct) filters
  • Conditional lead/lag function PostgreSQL?


Related Topics



Leave a reply



Submit