Split a column into two values in R [duplicate] - r

This question already has answers here:
Split data frame string column into multiple columns
(16 answers)
How to split column into two in R using separate [duplicate]
(3 answers)
Closed 4 years ago.
I want to split a dataframe containing only 1 column with below values into two columns containing only the numeric values
1: [0.426321243245,0.573678756755]
2: [0.413189679382,0.586810320618]
I have tried different ways in R using dplr -starts_with,seperate etc but couldn't split the column into dataframe containing two seperate columns.
Can someone please help me with this?
Thanks,

I hope this will help you
newdf <- read.table(text = "column1
0.426321243245,0.573678756755
0.413189679382,0.586810320618
", header = T)
library(splitstackshape)
final <- cSplit(newdf, 'column1', sep=",", type.convert=FALSE)

Related

Automate data frame width in R [duplicate]

This question already has answers here:
how to remove multiple columns in r dataframe?
(8 answers)
Select column 2 to last column in R
(4 answers)
Closed 2 years ago.
I have a data frame that I import from excel each week with 40+ columns. Each week the data frame has a different number of columns, I am only interested in the first 40. I take the data frame, drop the columns after 40 and rbind to another data frame (that has 40 columns).
The code I use to drop the columns is"
df = df[ -c(40:45) ] #assume df has 45 columns this week.
I would like to find a step to automate the lendth of columns to drop, similar to length(df$x) type of idea. I try width(df) but doesn't seem to work?
Any ideas please?

How can I order the rownames of a dataframe in R? [duplicate]

This question already has answers here:
How can I use the row.names attribute to order the rows of my dataframe in R?
(8 answers)
Closed 3 years ago.
I am using biological data from an imported CSV file where the gene symbols are put into "column 0" as rownames. I would like to order the rows alphabetically based on gene symbol.
I was thinking of extracting the column 0 rownames to a new column and then ordering but I prefer leaving the dataset how it is. Is there anyway to order the row names instead?
row_names_test <- data.frame(cbind(genes <- rownames(read.csv("~/row_names_test.csv")),
read.csv("~/row_names_test.csv")))
row_names_test <- row_names_test[order(row_names_test$genes), ]

How to Extract Two Columns of Data in R [duplicate]

This question already has answers here:
Extracting specific columns from a data frame
(10 answers)
Closed 4 years ago.
I have to extract two columns from this data set (Cars93 on MASS) and create a separate folder consisting only of the two columns MPG.highway and EngineSize. How do I go about doing this?
You can look at Cars93 on Mass and just get the first ten rows to see it.
You can create a subset using the names directly using the subset function or alternately,
new_df <- Cars93[,c("MPG.highway","EngineSize")]
#or
new_df <- subset(Cars93, keep = c("MPG.highway","EngineSize"))

Subsetting an R Matrix [duplicate]

This question already has answers here:
Extracting specific columns from a data frame
(10 answers)
Closed 4 years ago.
in R programming, how do I subset a matrix so that I can skip columns in between? I only know how to do it continuously such as 1:4, but what if I want the first, second, and fourth colum
You can select specific columns as follows:
new_df <- x[,c(1,2,4)]# Select column 1,2 and 4

replacing NA's in one column with the product of two other columns [duplicate]

This question already has answers here:
Replace NA in column with value in adjacent column
(3 answers)
Closed 4 years ago.
In a column of 7000 rows there are 11 NA's. I want to replace those NA's with the product of two other columns in my data frame
The column with NA's is TOTALCHARGES and the two columns I want to multiply are TENURE and MONTHLYCHARGES.
Find the indices of the missing data:
na.vals <- which(is.na(your_data$TOTALCHARGES))
Modify the relevant elements of TOTALCHARGES (within the data set):
your_data <- transform(your_data,
TOTALCHARGES=replace(TOTALCHARGES,na.vals,
TENURE[na.vals]*MONTHLYCHARGES[na.vals]))
Something like this (assuming df is your data.frame)?
df[is.na(df$TOTALCHARGES), "TOTALCHARGES"] <- df[is.na(df$TOTALCHARGES), "TENURE"] * df[is.na(df$TOTALCHARGES), "MONTHLYCHARGES"]

Resources