Is it possible to append a column to data frame in the following scenario?
dfWithData <- data.frame(start=c(1,2,3), end=c(11,22,33))
dfBlank <- data.frame()
..how to append column start from dfWithData to dfBlank?
It looks like the data should be added when data frame is being initialized. I can do this:
dfBlank <- data.frame(dfWithData[1])
but I am more interested if it is possible to append columns to an empty (but inti)
I would suggest simply subsetting the data frame you get back from the RODBC call. Something like:
df[,c('A','B','D')]
perhaps, or you can also subset the columns you want with their numerical position, i.e.
df[,c(1,2,4)]
dfBlank[1:nrow(dfWithData),"start"] <- dfWithData$start
Related
Hello, I have this type of table consisting of a single row and several columns. I have tried a code to extract my KD_PL parameters without success. Do you know a way in R to extract all the KD_PLs and store them in a vector or data frame array?
I tried this:
KDPL <- select("KD_PL.", which(substr(colnames(max_LnData), start=1, stop=6)))
This should do the trick:
library(tidyverse)
KDPL <- max_LnData %>% select(starts_with("KD_PL."))
This function selects all columns from your old dataset starting with "KD_PL." and stores them in a new dataframe KDPL.
If you only want the names of the columns to be saved, you could use the following:
KDPL_names <- colnames(KDPL)
This saves the column names in the vector KDPL_names.
I have a data frame (mydata) with >200 variables. I would like to automatically subset some of them into a smaller data frame.
The name of the variables I would like to subset follow a naming convention, e.g., "Q1Pct", "Q2Pct", ... "Q18Pct".
I can get a list of the variables using:
Q.names <- setNames(as.list(1:18),paste(paste0("mydata$Q",1:18,"Pct")))
I have tried to combine the list into a new data frame, but it isn't working:
df.QList <- data.frame(Q.names)
I'm sure there is a much better way to do this - please help.
You can try this:
library(dplyr)
select(mydata, all_of(paste0("Q",1:18,"Pct")))
Or, more simply (base R):
mydata[,paste0("Q",1:18,"Pct")]
I created a data set using a random row generator:
training_data <- fulldata[sample(nrow(fulldata),100,]
I am under the impression that I can create a second data set of the rest of the data ... rest_data <- fulldata[-training_data] is the code I jotted down in my notes but I am getting
"Error in '[.default'(fulldata, -training_data) :
What part of my code is incorrect?
assuming that fulldatais a dataframe you need a comma in the subscript to indicate that you want the rows of the data frame (i.e. fulldata[rows,columns]). But the indices of the new dataframe training_data will be numbered 1:100so you need a different sort of indicator that corresponds between training_dataand fulldata to show which rows of fulldata should not be included. What you might do is use the rownames, something like:
rest_data<-fulldata[-which(rownames(fulldata)%in%rownames(training_data)),]
which should tell R to remove the rownames of fulldata that occur in training_data. If you have something like an ID variable that is unique to each row you could also use this
rest_data<-fulldata[-which(fulldata$ID%in%training_data$ID),]
It has to be really simple but it looks like my mind is not working properly anymore.
So, what I would like to do is to store one of the columns from mtcars as a vector but after subsetting it. I need one line code for the subsetting and assigning a vector.
That's what I would like to achieve but with one line:
data <- mtcars[mtcars[,11]==4,]
vec <- data[,1]
Thx!
vec<-mtcars[mtcars[,11]==4,][,1]
The mtcars[,11]==4 would be the row index and by selecting the column index as '1', we get the first column with subset of rows based on the condition.
mtcars[mtcars[,11]==4, 1]
I have a list of 26 data frames called score.list and I have written a code that tells me which data frames are not complete. So this code gives me the name of the data frame within the list, but it doesn't tell me the index of the data frame in the list.
Example... the code tells me that a data frame named p08 and another data frame named p18 are not complete. Therefore, they need to combined with whichever data frame that follows after these. So if the data frame named p08 is score.list[[8]], then it should be combined with score.list[[9]]. It should replace [[8]] with the newly made data frame then score.list[[9]] should be deleted from the list.
I'm guessing something like the code below may work to combine & replace a data frame... I'm not sure if the following code works..
score.list[[8]] <- rbind(score.list[[8]], score.list[[9]])
This is what I tried doing... but didn't exactly work because it didn't make a new data frame after combining it. And I get this error message:
Error in if (names(score.list[i]) == names(score.list[i + 1])) { :
missing value where TRUE/FALSE needed
for(i in 1:length(score.list)){
if(names(score.list[i])==names(score.list[i+1])) {
a <- score.list[i]
b <- score.list[i+1]
score.list[[i]] <- rbind(a, b)
print(score.list[[i]])
}
}
Reason I wrote if(names(score.list[i]==names(score.list[i+1])) as that is because the names of the data frames that need to be combined together are the same in the list. The data frame that is not complete has the same name as the one that follows it. So name of the data frame score.list[[8]] is same as the name of the data frame score.list[[9]].
Please let me know if there are confusing parts.. I tried to write it as clear as I can. Thank you!
This should help you :
## a list example
score.list <-
list(l1= data.frame(x=1),
l2=data.frame(x=2),
l3= data.frame(x=3))
## use %in% to select some elements
## here I am selecting list l1 and l3
do.call(rbind,
score.list[names(score.list) %in% c('l1','l3')])