3

このデータセットの 4 つの文字変数を順序付き因子に変換しようとしています。私が適用しようとしているロジックは、以下の変数「A」に示されています。

df$A = factor(ifelse(df$A %in% c('NOT CONDUCTED AT ALL','RARELY'),'L1',
                              ifelse(df$A == 'OCCASIONALLY', 'L2',
                                     ifelse(df$A == 'QUITE FREQUENTLY', 'L3', 'L4'))))
df$A = ordered(factor(df$A), levels=c('L1','L2','L3','L4'))

同じ条件を使用して、すべての変数を一度に変換する方法はありますか?

4

3 に答える 3

4
# first, make sure each column is an ordered factor with all of the levels
# if the columns of df are character, replace levels(unlist(df)) with unique(unlist(df))
df <- data.frame(lapply(df, factor, ordered=TRUE, levels=levels(unlist(df))))
# create mapping to the new levels following the order 
# imposed in the previous step
new.lvl.mapping <- c('L4', 'L2', 'L3', 'L1', 'L1')
# make the replacement using the mapping
data.frame(lapply(df, function(col) new.lvl.mapping[col]))
于 2013-05-29T15:52:48.543 に答える
1

必要な係数を作成する関数を定義し、それを を使用して必要なデータ フレームの各列に一度に適用できますsapply()

# some fake data for example
dat <- c("NOT CONDUCTED AT ALL", "RARELY", "OCCASIONALLY", "QUITE FREQUENTLY", "ALWAYS")
df <- data.frame(A=sample(dat, 25, TRUE), B=sample(dat, 25, TRUE), D=rnorm(25))
head(df)
                     A                B           D
1 NOT CONDUCTED AT ALL QUITE FREQUENTLY -0.04049165
2     QUITE FREQUENTLY QUITE FREQUENTLY  0.74361906
3               ALWAYS     OCCASIONALLY -0.93606555
4               ALWAYS           ALWAYS  0.56659322
5               RARELY     OCCASIONALLY  0.97216491
6     QUITE FREQUENTLY     OCCASIONALLY  0.91125383

# define a function to create a new factor variable
newfac <- function(x, oldval, newval, ordered=TRUE) {
    factor(newval[match(x, oldval)], ordered=TRUE)
    }

# apply the function to each specified element of the data frame
df[, c("A", "B")] <- sapply(df[, c("A", "B")], newfac, 
    oldval=c("NOT CONDUCTED AT ALL", "RARELY", "OCCASIONALLY", "QUITE FREQUENTLY", "ALWAYS"), 
    newval=c("L1", "L1", "L2", "L3", "L4")
    )
head(df)
   A  B           D
1 L1 L3 -0.04049165
2 L3 L3  0.74361906
3 L4 L2 -0.93606555
4 L4 L4  0.56659322
5 L1 L2  0.97216491
6 L3 L2  0.91125383
于 2013-05-29T16:08:02.997 に答える
0

入れ子にするのではなく、別のdata.frameものを作成しますmergeifelse

df2 <- data.frame(New_A = c("L1", "L1", "L2", "L3", "L4"),
                  Old_A = c("NOT CONDUCTED AT ALL", "RARELY", 
                            "OCCASIONALLY", "QUITE FREQUENTLY", "ALWAYS"))

df3 <- merge(df, df2, by.x="A", by.y="Old_A")
df$A <- df3$New_A

すべての変数を変換する限り、次のようにできますapply

apply(df, 2, function(X) ...

各列は次のように関数に渡されますX

于 2013-05-29T15:42:52.643 に答える