Data extraction in R - multiple columns - r

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.

Related

How can I create a one-column dataframe containing all numeric values of a dataframe with several columns?

Sorry for the basic question here!
I am working the following type of dataset:
data <- data.frame(a=c(1,2,3), b=c(2,3,4), c=(10,11,12))
I am trying to turn it into a new data frame where there is only one column, containing all of these values in individual rows, i.e. a=c(1,2,3,2,3,4,10,11,12)
What function should I use to arrive at this output?
All the best,
Cameron
You can do data.frame(a = unlist(data)).

create a vector containing row IDs of dataframe

I am using one of Rs built in datasets called USArrests. It looks like instead of the rows having a numeric ID, they have a State as the row ID. Now how do I create a vector containing all of these state names?
I would generally use myvec <- c(USArrests$colname) but I am not sure how to access the states as it is not considered a normal column
data("USArrests")
head(USArrests)
vector_of_names <- rownames(USArrests)
##if you want to append to the dataframe
USArrests$state_name <-rownames(USArrests)
USArrests

Find data by multiple variable names in R

I have a question regarding variables names in R.
In my dataset I have a list of 70 variable names as characters and I want to find the corresponding data (including the header) in the data.
For example I used the dataset iris. I don't want to select all variables by iris$Sepal.Length since I have 70 variables in the dataset that I use. In my code I can print the data but I am struggling with saving the data as a dataframe with the corresponding header names. Somebody any thoughts?
iris
head(iris)
colnames(iris)
b <- list("Sepal.Length","Petal.Length")
i=1
for (i in 1:length(b)){
#print(b[[i]])
print(iris[,c(b[[i]])])
c[,i]<-(iris[,c(b[[i]])])
}
It sounds like you're trying to get a subset of 70 columns from a data.frame or matrix. The 70 columns you have are stored in a list. R will let you get columns named by a character vector, but not by a list. So, you can just use unlist.
b <- list("Sepal.Length","Petal.Length")
newTable <- iris[,unlist(b)]
I find dplyr the best for this. If you turn iris into a tibble
iris <- as_tibble(iris)
You can then use the dplyr::select function either selecting by name (no quotes) or by position. You can even use the 1:5 notation selecting columns 1 to 5. A great place to start is: http://r4ds.had.co.nz
Are you looking for this ?
b <- c("Sepal.Length","Petal.Length")
New_iris=iris[,b]

R: changing entries of a dataset

I have to summarize the information of an AnnotatedDataFrame and a graphNEL together. My problem is that the same "feature" in the data frame and in the graphNEL has different names: in the data frame I have NAME and IDnumber separately (two columns), in the graphNEL I have a single column whose entries are in the form NAME(IDnumber). It is a very large dataset, so it is absolutely out of question to rename them manually. Is there a way to make the dataset entries in the form NAME(IDnumber)?
Thank you in advance.
Supposing your dataframe is called df, you can create a new variable with:
df$new <- paste0(df$NAME, "(", df$IDnumber, ")")

Appending column to a data frame - R

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

Resources