This question already has answers here:
How do you create a list with a single value in R?
(2 answers)
Closed 5 years ago.
Is there a more elegant way to create a list consisting only of zeros (I don't want to use vector("list", <len>) because it creates NULL entries)?
lapply((1:5), function(x) 0)
(Sorry, this is probably a duplicate, but I couldn't find the answer ..)
Thx & kind regards
We can use the as.list with numeric or integer
as.list(numeric(5))
We can use rep like this
rep(0,5)
Related
This question already has answers here:
How to remove columns with same value in R
(4 answers)
Closed 2 years ago.
I have a really large dataset and I want to filter out some of the columns because it is the same data all throughout (ex: company name is all "Walmart"). I can go through and do these manually but I'm looking for a code to do it automatically.
I had in mind a function to subset based on if sum(unique(colnam)) == 1 but not sure how to get it to work. Thanks.
which(sapply(dat, function(col) length(unique(col)) == 1))
This question already has answers here:
Extracting numbers from vectors of strings
(12 answers)
Closed 3 years ago.
what is the most easiest way how to get number from string? I have huge list of links like this, I need to get that number 98548 from it.
https://address.com/admin/customers/98548/contacts
Note that number cant have different count of numbers and can start from 0 to 9
This is the most easiest that I know :
str <- "https://address.com/admin/customers/98548/contacts"
str_extract_all(str, "\\d+")[[1]]
Using stringr:
no="https://address.com/admin/customers/98548/contacts"
unlist(stringr::str_extract_all(no,"\\d{1,}"))
[1] "98548"
This question already has answers here:
Generate a dummy-variable
(17 answers)
Closed 5 years ago.
Beginner in R and looking to avoid unnecessary copy+pasting...
I have a data frame with a numeric column. I would like to create binary columns based on the values in the numeric column.
I know the tedious approach would be to copy+paste the following and manually add the different values:
DataFrame$NewCol1 <- as.numeric(DataFrame$ExistingCol == 1);
DataFrame$NewCol2 <- as.numeric(DataFrame$ExistingCol == 2);
Would a "for" loop be able to accomplish this task?
How about something like this?
model.matrix(~factor(DataFrame$ExistingCol))[,-1]
This question already has answers here:
Moving columns within a data.frame() without retyping
(17 answers)
Closed 9 years ago.
I'd like to reorganize my data frame. I just wanted to move the last column into first place and the rest leave in the same order. I used function subset to do it. It works but it would be painful if I have like 100 columns or so.
Is there any easier way to do it ?
tbl_comp <- subset(tbl_comp, select=c("Description","Meve_mean","Mmor_mean", "Mtot_mean", "tot_meanMe", "tot_meanMm", "tot_sdMe", "tot_sdMm", "Wteve_mean", "Wtmor_mean", "Wttot_mean", "tot_meanwte", "tot_meanwtm", "tot_sdwte", "tot_sdwtm"))
Try this
tbl_comp <- subset(tbl_comp, select=c(Description , Meve_mean:tot_sdwtm))
tbl_comp <- cbind(tbl_comp[ncol(tbl_comp)], tbl_comp[-ncol(tbl_comp)])
will do the trick.
This question already has answers here:
do.call(rbind, list) for uneven number of column
(4 answers)
Closed 9 years ago.
There are objects with basic datas like a<-list(a=1,b="A",c=character())
Now I want convert it to a data.frame, but there for I need equal rows. How to fill the empty vectors with NA in easy way to run as.data.frame(a)? the only Idea I have is to ask if one elment of the list has length<1 then set element[1]=NA.
I'm not sure this is any cleaner, but it does get rid of the if stuff:
lfoo<-list(one=1:3,two=character(),three=4:6,four=vector())
dfoo<-dfoo<-data.frame(one=rep(NA,3),two=rep(NA,3), three=rep(NA,3),four=rep(NA,3))
lvalues <- which(unlist(lapply(1:4,function(x) length(lfoo[[x]]) > 0))
for (j in lvalues) dfoo[,jvalues]<-lfoo[[jvalues]]
This may point you to simple ways of dealing with conversions and selective replacements.