Automatically generate a character vector with 100 elements - r

I would like to automatically create a vector with the following elements:
elements<-c("elem[1]","elem[1]" .... "elem[100]")
without typing elem[1], elem[2] etc by hand. How can I do this automatically?
Thanks

You can use paste0():
#Code
paste0('elem[',1:100,']')

Related

Is there a way to print out single values from a multidimentional list? in python please

Imagine i have a multidimensional List like this vals = [['John', '20'], ['Derron', '5'], ['Mike', '43']], what can i do to print out only the names e.g: John, Derron, Mike
In which language you want to do this? Using JavaScript you can try below solution.
function print(val) {
console.log(val[0])
}
vals.forEach(print)
You can use a nested for loop. In python For example:
for sublist in vals:
print(sublist[0])
The first line of this code will loop through each sublist. For this question, ['John','20'] is a sublist. The second line will print out the first element (aka name) of each of these sublists.

Converting character to numeric in R

I Have dataset like below which I am trying to convert column "Installs" to numeric, my codes are like below:
Original Dataset
My Codes:-
Data$Installs<-substr(Data$Installs,1,nchar(Data$Installs)-1)
Data$Installs<-gsub(",","",gsub("\\s+","",Data$Installs))
Data$Installs<-as.numeric(Data$Installs)
after the code I get below
This is the result I get
Any help?
From what I can see, you need only to remove commas and a possible trailing plus sign. So, the following should work:
Data$Installs <- as.numeric(gsub("[+,]", "", Data$Installs))
You might want to create a new column though and keep the original one.

converting the 13-,4- to -13,-4 using regular expression?

So I have a text column in data frame:
stocksavailable
140,13-,3-,40-,2-
The numbers 13-, 2- and 3- are incorrect while extracting, can we get something like this using R code?
stocksavailable
140,-13,-3,-40,-2
Assuming the numbers are integers, try: (\d+)(\-)?
Demo
You should be able to use the gsub method like this

Dynamically assign elements to object

In R, I am trying to dynamically select columns of a data.frame called DF. If
cutOffYear=2014
and
forecast_years=3
Then this piece of code
paste0("DF$X",cutOffYear+1:forecast_years)
yields:
[1] "DF$X2015" "DF$X2016" "DF$X2017"
Assuming all three columns exist in DF how do I assign the column variables to the characters?
I have tried a lot of combinations of get, assign and paste0 but I am failing.
We can try with [ to select the columns. It is often error prone when using $. If we need to get the output as a data.frame with columns specified in the pasted combination of 'cutOffYear', 'forecast_years', then the below code should work fine
DF[paste0("X", cutOffYear+1:forecast_years)]

replicate a string with minor changes without a for loop

I have a couple of columns named "Lab1Date", "Lab3Date" "Lab7Date" etc and more column of the same pattern - the integer changes but not the rest of the string. I can generate a vector with such column names using a for loop easily, like
for (j in c(1,3,7,14,28)) {
newcolorder <- c(newcolorder,paste0("Lab",j,"Date"))
}
But I was wondering whether there was a more elegant, idiomatic way in R, maybe using the likes of rep().
Thanks.
You can use paste directly without a for loop as paste is vectorized.
paste0('Lab', c(1,3,7, 14,28), 'Date')

Resources