Code to rename multiple columns in rStudio - r

I want to rename this columns in R, I want to remove X from each of them so that it remains just figures which represents different years varying from 1960 to 2020. The first two (country name and Country Code) are sorted out already.
[1] "ï..Country.Name" "Country.Code" "X1960" "X1961" "X1962"
[6] "X1963" "X1964" "X1965" "X1966" "X1967"
[11] "X1968" "X1969" "X1970" "X1971" "X1972"
[16] "X1973" "X1974" "X1975" "X1976" "X1977"
[21] "X1978" "X1979" "X1980" "X1981" "X1982"
[26] "X1983" "X1984" "X1985" "X1986" "X1987"
[31] "X1988" "X1989" "X1990" "X1991" "X1992"
[36] "X1993" "X1994" "X1995" "X1996" "X1997"
[41] "X1998" "X1999" "X2000" "X2001" "X2002"
[46] "X2003" "X2004" "X2005" "X2006" "X2007"
[51] "X2008" "X2009" "X2010" "X2011" "X2012"
[56] "X2013" "X2014" "X2015" "X2016" "X2017"
[61] "X2018" "X2019" "X2020"

names(df) <- gsub("^X", "", names(df))
gsub() matches a regular expression and replaces it if found. In our case, the regex says the string must have an X at the beginning.

Related

Using R, How do I copy the tibble to an element of the list. for example, each element like ff[i] have a nibble at each i

Use vector() to create an empty vector called ff that is of mode “list” and length 9. Now write a for() loop to loop over the 9 files in dfiles and for each (i) read the file in to a tibble, and change the column names to x and y, and (ii) copy the tibble to an element of your list ff.
dfiles is a directory which has different files.
This is what I did.
ff <- vector(mode = "list", length = 9)
length <- length(dfiles)
for (i in 1:length) {
study <- read_csv(dfiles[i])
names(study)[1] <- "x"
names(study)[2] <- "y"
ff[i] <- c(study)
print(head(ff[i]))
}
[[1]]
[1] -0.989532202 -0.052799402 0.823610903 -0.255509103 -0.220684347
[6] 0.307726791 -0.060013253 -0.555652890 -0.138615019 1.882839792
[11] 0.873668680 -0.914597073 -1.244917622 -0.359982241 1.328774701
[16] 0.292679118 -0.701505237 0.882234568 -0.133370389 -1.120678499
[21] 0.461192454 1.524142810 0.434468298 0.192000371 -0.656243128
[26] 0.568398531 -1.070570535 -1.653149024 -0.043352768 -0.034593506
[31] 2.365055532 -1.216347308 0.170906323 0.805053094 1.050592844
[36] -0.010724485 -0.743256141 -0.065784052 1.939755992 0.482739008
[41] -2.044477073 1.423459129 0.540502661 -0.033571772 -0.017863621
[46] -0.149789720 0.256559481 -0.503866933 0.277011252 -0.931356025
[51] 0.200146875 1.106837421 0.509206114 1.033749676 -1.090868762
[56] 0.054792784 0.617250303 -1.068004868 1.565814337 -1.034808011
[61] 0.164518709 0.151832330 0.121670302 -0.210424584 0.449936787
[66] -1.031164492 -1.289364188 -0.654568638 -0.057324104 1.256747820
[71] 1.587454140 0.319481463 0.381591623 -0.243644884 0.048053084
[76] -1.404545861 0.289933729 -0.535553582 0.334678773 -0.345981339
[81] -0.661615735 -0.219111377 -0.366904911 1.094578208 0.209208082
[86] 0.432491426 -1.240853586 1.496821710 0.159370441 -0.856281403
[91] 0.309046645 0.870434030 -1.383677138 1.690106970 -0.158030705
[96] 1.121170781 0.072261319 -0.332422845 -1.834920047 -1.100172219
[101] -0.041340300 0.827852545 -1.881678654 1.375441112 1.398990464
[106] -1.143316256 0.472300562 -1.033639213 -0.125199979 0.928662739
[111] 0.868339648 -0.849174604 -0.386636454 -0.976163571 0.339543660
[116] -1.559075164 -2.629325442 1.469812282 2.273472913 -0.455033540
[121] 0.761102487 -0.007502784 1.474313800
and the following error.
1: In ff[i] <- c(study) :
number of items to replace is not a multiple of replacement length
2: In ff[i] <- c(study) :
I was expecting that it'll still have column names so I am not sure how to fix it and where I am going wrong.
Was supposed to use double brackets.
ff[[i]] <- study would fix the problem.

reorder a 1 dimensional dataframe based on the column order of a larger dataframe (R)

relevant_ods_reordered <- relevant_ods[names(cpm)]
the above seeks to reorder column names of a dataframe relevant_ods:
Plate1_DMSO_A01 Plate1_DMSO_B01 Plate1_DMSO_C01 Plate1_Lopinavir_D01
OD595 0.431 0.4495 0.4993 0.5785
Plate1_DMSO_E01 Plate1_DMSO_F01 Plate1_DMSO_G01 Plate1_DMSO_H01
OD595 0.5336 0.5133 0.527 0.5413
Plate1_DMSO_C12 Plate1_DMSO_D12 Plate1_Lopinavir_E12 Plate1_DMSO_F12
OD595 0.4137 0.4274 0.5241 0.4264
Plate1_DMSO_G12 Plate1_DMSO_H12
OD595 0.4561 0.4767
to match the order of the columns in a significantly larger dataframe:
[1] "Plate1_DMSO_A01" "Plate1_DMSO_A12"
[3] "Plate1_DMSO_B01" "Plate1_DMSO_B12"
[5] "Plate1_DMSO_C01" "Plate1_DMSO_C12"
[7] "Plate1_DMSO_D12" "Plate1_DMSO_E01"
[9] "Plate1_DMSO_F01" "Plate1_DMSO_F12"
[11] "Plate1_DMSO_G01" "Plate1_DMSO_G12"
[13] "Plate1_DMSO_H01" "Plate1_DMSO_H12"
[15] "Plate1_Lopinavir_D01" "Plate1_Lopinavir_E12"
[17] "Plate1_NS1519_22009_A02" "Plate1_NS1519_22009_A04"
[19] "Plate1_NS1519_22009_A05" "Plate1_NS1519_22009_A06"
[21] "Plate1_NS1519_22009_A07" "Plate1_NS1519_22009_A08"
[23] "Plate1_NS1519_22009_A09" "Plate1_NS1519_22009_A10"
[25] "Plate1_NS1519_22009_A11" "Plate1_NS1519_22009_B02"
[27] "Plate1_NS1519_22009_B03" "Plate1_NS1519_22009_B04"
[29] "Plate1_NS1519_22009_B05" "Plate1_NS1519_22009_B06"
etc.
Clearly, there is a returned
Error in `[.data.frame`(relevant_ods, names(cpm)) :
undefined columns selected
due to the mismatch between the numbers of columns
I have tried
relevant_ods_reordered <- relevant_ods[names(cpm),]
relevant_ods_reordered <- select(relevant_ods, names(cpm))
relevant_ods_reordered <- match(relevant_ods, names(cpm))
With base R, you need to find the names in common. intersect is good for this and preserves the order of its first argument:
relevant_ods[intersect(names(cpm), names(relevant_ods))]
Or with dplyr, use the select helper any_of:
select(relevant_ods, any_of(names(cpm)))

list files pattern select date

Hello I have a set of daily meteo data, using the expression :
f <- list.files(getwd(), include.dirs=TRUE, recursive=TRUE, pattern= "PREC")
I select only the files of Precipitation
I wonder how to select only files for example of January, the one for example named 20170103 (yyyymmdd) , so the one named yyyy01dd....
the files are named in this way: "PREC_20010120.grd".
Try pattern='PREC_\\d{4}01\\d[2].*'.
PREC_ literally
\\d{4} four digits
01 '"01" literally
\\d{2} two digits
.* any character repeatedly
Thank you , but I retrieved only 35 items instead of 31 days * 10 years what's wrong ?
[1] "20100102/PREC_20100102.tif" "20100112/PREC_20100112.tif"
[3] "20100122/PREC_20100122.tif" "20110102/PREC_20110102.tif"
[5] "20110112/PREC_20110112.tif" "20110122/PREC_20110122.tif"
[7] "20120102/PREC_20120102.tif" "20120112/PREC_20120112.tif"
[9] "20120122/PREC_20120122.tif" "20130102/PREC_20130102.tif"
[11] "20130112/PREC_20130112.tif" "20130122/PREC_20130122.tif"
[13] "20140102/PREC_20140102.tif" "20140112/PREC_20140112.tif"
[15] "20140122/PREC_20140122.tif" "20150102/PREC_20150102.tif"
[17] "20150112/PREC_20150112.tif" "20150122/PREC_20150122.tif"
[19] "20160102/PREC_20160102.tif" "20160112/PREC_20160112.tif"
[21] "20160122/PREC_20160122.tif" "20170102/PREC_20170102.tif"
[23] "20170112/PREC_20170112.tif" "20170122/PREC_20170122.tif"
[25] "20180102/PREC_20180102.tif" "20180112/PREC_20180112.tif"
[27] "20180122/PREC_20180122.tif" "20190102/PREC_20190102.tif"
[29] "20190112/PREC_20190112.tif" "20190122/PREC_20190122.tif"
[31] "20200102/PREC_20200102.tif" "20200112/PREC_20200112.tif"
[33] "20200122/PREC_20200122.tif" "20210102/PREC_20210102.tif"
[35] "20210112/PREC_20210112.tif" "20210122/PREC_20210122.tif"
Resolved with:
f <- list.files(getwd(), include.dirs=TRUE, recursive=TRUE, pattern='PREC_\\d{4}01.*')

R sort list of files numerically [duplicate]

This question already has answers here:
How to sort a character vector where elements contain letters and numbers?
(6 answers)
Closed 2 years ago.
I have a list of files that I need to sort numerically, such that I can import them in order
my code is:
bed = '/files/coverage_v2'
beds=list.files(path=bed, pattern='ctcf.motif.minus[0-9]+.bed.IGTB950.bed')
for(b in beds){
`for(b in beds){`print(b)
read.table(b)
}
> [1] "ctcf.motif.minus1.bed.IGTB950.bed" "ctcf.motif.minus10.bed.IGTB950.bed"
[3] "ctcf.motif.minus100.bed.IGTB950.bed" "ctcf.motif.minus101.bed.IGTB950.bed"
[5] "ctcf.motif.minus102.bed.IGTB950.bed" "ctcf.motif.minus103.bed.IGTB950.bed"
[7] "ctcf.motif.minus104.bed.IGTB950.bed" "ctcf.motif.minus105.bed.IGTB950.bed"
[9] "ctcf.motif.minus106.bed.IGTB950.bed" "ctcf.motif.minus107.bed.IGTB950.bed"
[11] "ctcf.motif.minus108.bed.IGTB950.bed" "ctcf.motif.minus109.bed.IGTB950.bed"
[13] "ctcf.motif.minus11.bed.IGTB950.bed" "ctcf.motif.minus110.bed.IGTB950.bed"
[15] "ctcf.motif.minus111.bed.IGTB950.bed" "ctcf.motif.minus112.bed.IGTB950.bed"
[17] "ctcf.motif.minus113.bed.IGTB950.bed" "ctcf.motif.minus114.bed.IGTB950.bed"
[19] "ctcf.motif.minus115.bed.IGTB950.bed" "ctcf.motif.minus116.bed.IGTB950.bed"
[21] "ctcf.motif.minus117.bed.IGTB950.bed" "ctcf.motif.minus118.bed.IGTB950.bed"
[23] "ctcf.motif.minus119.bed.IGTB950.bed" "ctcf.motif.minus12.bed.IGTB950.bed"
[25] "ctcf.motif.minus120.bed.IGTB950.bed" "ctcf.motif.minus121.bed.IGTB950.bed"
[27] "ctcf.motif.minus122.bed.IGTB950.bed" "ctcf.motif.minus123.bed.IGTB950.bed"
[29] "ctcf.motif.minus124.bed.IGTB950.bed" "ctcf.motif.minus125.bed.IGTB950.bed"
[31] "ctcf.motif.minus126.bed.IGTB950.bed" "ctcf.motif.minus127.bed.IGTB950.bed"
[33] "ctcf.motif.minus128.bed.IGTB950.bed" "ctcf.motif.minus129.bed.IGTB950.bed"
[35] "ctcf.motif.minus13.bed.IGTB950.bed" "ctcf.motif.minus130.bed.IGTB950.bed"
[37] "ctcf.motif.minus131.bed.IGTB950.bed" "ctcf.motif.minus132.bed.IGTB950.bed"
[39] "ctcf.motif.minus133.bed.IGTB950.bed" "ctcf.motif.minus134.bed.IGTB950.bed"
But what I really want is for it to be sorted numerically:
> "ctcf.motif.minus1.bed.IGTB950.bed"
"ctcf.motif.minus10.bed.IGTB950.bed"
"ctcf.motif.minus11.bed.IGTB950.bed"
"ctcf.motif.minus12.bed.IGTB950.bed"
"ctcf.motif.minus13.bed.IGTB950.bed"
"ctcf.motif.minus100.bed.IGTB950.bed"
"ctcf.motif.minus101.bed.IGTB950.bed"
etc, so that it will be imported numerically.
Thanks in advance!!
You could try mixedsort from gtools
library(gtools)
beds1 <- mixedsort(beds)
head(beds1)
#[1]"ctcf.motif.minus1.bed.IGTB950.bed" "ctcf.motif.minus10.bed.IGTB950.bed"
#[3]"ctcf.motif.minus11.bed.IGTB950.bed" "ctcf.motif.minus12.bed.IGTB950.bed"
#[5]"ctcf.motif.minus13.bed.IGTB950.bed" "ctcf.motif.minus100.bed.IGTB950.bed"
Or using regex (assuming that the order depends on the numbers after 'minus' and before 'bed'.
beds[order(as.numeric(gsub('\\D+|\\.bed.*', '', beds)))]

How to write a list to file as one row and without quotes in R

I am trying to write a list to file as one row and without quotes in R.
Content of the list is:
[1] "X4775495036_J" "X4775495036_F" "X5147722015_F" "X5067554009_F"
[5] "X5067554063_B" "X4954590047_A" "X5067554063_G" "X5067554009_L"
[9] "X5147722015_D" "X5511045011_D" "X5067554063_A" "X4805447025_F"
[13] "X5455362015_K" "X4805447025_L" "X5147722015_B" "X5067554009_G"
[17] "X5147722014_K" "X5067554063_H" "X5147722009_G" "X5067554008_H"
[21] "X5067554054_H" "X4805447016_K" "X5147722014_E" "X4954590051_K"
[25] "X5067554008_E" "X5147722015_H" "X5147722009_H" "X5067554063_D"
[29] "X5147722015_A" "X5511045022_E" "X5067554054_I" "X5067554063_J"
[33] "X5067554007_F" "X4775495036_E" "X4775495036_H" "X4805447025_H"
[37] "X5067554009_I" "X4805447025_K" "X4954590051_C" "X4805447025_E"
[41] "X5067554063_E" "X5147722009_J" "X5067554054_C" "X5067554054_G"
[45] "X4805447016_I" "X5455362015_B" "X5067554009_H" "X5147722014_A"
[49] "X4775495036_I" "X5067554063_L" "X5455362015_J" "X4954590047_J"
[53] "X5067554009_A" "X4954590051_D" "X5455362015_I" "X5511045011_E"
[57] "X5147722014_F"
I want something like this (all elements in one row):
X4775495036_J X4775495036_F X5147722015_F X5067554009_F ...
I have tried with write.table, write but with no result.
Note that you don't have a list, you have a character vector.
cat(your_vector, "\n", file="your_file.txt")
The "\n" is an optional newline at the end.
You could use the ncolumns argument of write:
n <- LETTERS[1:10] # create example values
write(n, "letters.txt", ncolumns=length(n))
Or you could concatenate your names before:
nc <- paste0(n, collapse=" ")
write(nc, "letters.txt")

Resources