alan
alan

Reputation: 87

Remove first X characters in a data.frame column

I have a data frame like below,

DATA[,1]

Price=100
Price=200
Price=300

DATA[,2]

Size=10
Size=20
Size=30

What's the easy way I can remove the character part then do the calculation directly.

I would expect to have result like:

100*10
200*20
300*30

Upvotes: 0

Views: 10080

Answers (2)

Christopher John
Christopher John

Reputation: 4490

  1. Use df$c1 <- gsub("Size=", "", df$c1) on both columns to remove the characters
  2. Use df$c1 <- as.numeric(as.character(df$c1)) on both columns to change to numeric
  3. df$c3 <- df$c1*df$c2 will work to multiply the columns and create a new column of answers

Upvotes: 3

majom
majom

Reputation: 8021

Here is a solution using substring to get right of the left hand side of each column.

The assumption is here that the part you want to cut out (e.g. "Price=") has the same length within one column while the length of the numeric value at the end can vary (e.g. "100" or "1000000").

# Create sample data
d <- data.frame(column1=c("Price=100", "Price=200", "Price=300", "Price=400"),
                column2=c("Size=10","Size=20","Size=30", "Size=40"),
                stringsAsFactors=FALSE)

# Transform and multiply columns
d$result <- as.numeric(substring(d[,1], 7, nchar(d[,1]))) * 
            as.numeric(substring(d[,2], 6, nchar(d[,2])))

# Result
# > d
# column1     column2 result
# 1 Price=100 Size=10   1000
# 2 Price=200 Size=20   4000
# 3 Price=300 Size=30   9000
# 4 Price=400 Size=40  16000

Upvotes: 0

Related Questions