计算R中的字符值

时间:2017-04-17 05:27:20

标签: r

我有一个如下所示的数据框。

data <- data.frame(Var_1 = c("A","B","C","A","B"))

Var_1
A
B
C
A
B

需要像下面这样计算。

Var_1 Count
A     2
B     2
C     1
A     2
B     2

2 个答案:

答案 0 :(得分:2)

# sample data
df <- data.frame(Var_1 = c("A","B","C","A","B"))

# make a frequency table to determine the "count"
countsDF <- table(df$Var_1)

# use names to match the Var_1 in the countsDF, then assign
# the corresponding count
df$count <- countsDF[match(df$Var_1,  names(countsDF))]

答案 1 :(得分:1)

我相信您可以尝试使用该表并将其存储为数据帧,您可以从此数据框中访问数据的频率。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum { MAXC = 128, MAXW = 1000 };

int main (void) {

    char *words[MAXW] = {NULL},   /* storage array of pointers to char* */
        tmp[MAXC] = "";
    int freq[MAXW] = {0}, n = 0;  /* simple integer frequency array */

    /* while < MAXW unique words, read each word in file */
    while (n < MAXW && fscanf (stdin, " %s", tmp) == 1) {
        int i;

        for (i = 0; words[i]; i++)   /* check against exising words */
            if (strcmp (words[i], tmp) == 0)    /* if exists, break */
                break;

        if (words[i]) {     /* if exists */
            freq[i]++;      /* update frequency */
            continue;       /* get next word */
        }

        /* new word found, allocate storage (+ space for nul-byte) */
        words[n] = malloc (strlen (tmp) + 1);
        if (!words[n]) {    /* validate ALL allocations */
            fprintf (stderr, "error: memory exhausted, words[%d].\n", n);
            break;
        }

        strcpy (words[n], tmp); /* copy new word to words[n] */
        freq[n]++;              /* update frequency to 1 */
        n++;                    /* increment word count */
    }

    for (int i = 0; i < n; i++) {                 /* for each word */
        printf ("%s = %d\n", words[i], freq[i]);  /* output word + freq */
        free (words[i]);           /* free memory when no longer needed */
    }

    return 0;
}

PS:我不确定你是否要重复你问题中提到的数据的“级别”,但除非是特定于案例(未提及),否则我会考虑重复的课程或级别。