Replacing data frame values ​​with their corresponding sum

How to replace "0 | 0", "0 | 1", "1 | 0", "1 | 1", "2 | 0" by "0", "1", "1", 2 "," 2 " , respectively, in the data frame? For example:

df1 <- data.frame(A = c("0|0", "1|1", "0|1"), B = c("2|0", "0|0", "1|0"))

Expected results are the sum of each pair, that is:

df2 <- data.frame(A = c("0", "2", "1"), B = c("2", "0", "1"))
+4
source share
3 answers

Here is an attempt. If you need a second data frame, first use df2 <- df1so that you have the old and the new ( df2will be old), or wrap it with as.data.frame()and name it df2.

df1[] <- lapply(df1, function(x) {
    ## split the column on '|'
    s <- strsplit(as.character(x), "|", fixed = TRUE)
    ## coerce to numeric and find the sum
    vapply(s, function(a) sum(as.numeric(a)), 1)
})

which gives

df1
#   A B
# 1 0 2
# 2 2 0
# 3 1 1

, . . , , .

1: strsplit() scan() .

df1[] <- lapply(df1, function(x) { 
    vapply(as.character(x), function(a) sum(scan(text = a, sep = "|")), 1)
})

2: , . , df1.

rs <- rowSums(read.table(text = as.matrix(df1), sep = "|"))
dim(rs) <- dim(df1)
dimnames(rs) <- dimnames(df1)
as.data.frame(rs)
#   A B
# 1 0 2
# 2 2 0
# 3 1 1

, ...

as.data.frame(
    `dimnames<-`(
        `dim<-`(
            rowSums(read.table(text = as.matrix(df1), sep = "|")), 
            dim(df1)
        ), 
        dimnames(df1)
    )
)
#   A B
# 1 0 2
# 2 2 0
# 3 1 1
+4
df1_split <- lapply(df1, function(x){strsplit(as.character(x), split = "\\|")})
df1_sum <- lapply(df1_split, lapply, function(x) sum(as.numeric(x)))
as.data.frame(lapply(df1_sum, unlist))

  A B
1 0 2
2 2 0
3 1 1
+2

gsub dplyr.

df1 <- data.frame(A = c("0|0", "1|1", "0|1"), B = c("2|0", "0|0", "1|0"),
                  stringsAsFactors = FALSE)
library(dplyr)
df1 %>% mutate(A=as.numeric(gsub("\\|.+", "", A))+as.numeric(gsub(".+\\|", "", A)), 
               B=as.numeric(gsub("\\|.+", "", B))+as.numeric(gsub(".+\\|", "", B)))
#   A B
# 1 0 2
# 2 2 0
# 3 1 1
0

Source: https://habr.com/ru/post/1620219/


All Articles