How to find the sin of each value in columns if some columns are categorical in R data frame?


To find the sin of each value in columns if some columns are categorical in R data frame, we can follow the below steps −

  • First of all, create a data frame.

  • Then, use numcolwise function from plyr package to find the sin of each value in columns if some columns are categorical.

Example

Create the data frame

Let’s create a data frame as shown below −

Level<-sample(c("low","medium","high"),25,replace=TRUE)
Group<-sample(c("first","second"),25,replace=TRUE)
DV1<-sample(1:5,25,replace=TRUE)
DV2<-sample(1:5,25,replace=TRUE)
df<-data.frame(Level,Group,DV1,DV2)
df

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

   Level  Group DV1 DV2
1  low    first  1  1
2  low    second 1  5
3  medium first  5  3
4  low    first  4  1
5  low    second 2  5
6  low    first  3  3
7  high   second 3  1
8  low    second 1  1
9  high   first  2  4
10 low    second 1  3
11 high   first  2  5
12 low    first  3  2
13 medium second 5  4
14 low    second 5  4
15 low    second 3  4
16 medium first  5  5
17 high   second 4  5
18 high   second 4  1
19 low    first  4  4
20 medium second 1  3
21 high   second 4  3
22 medium first  1  3
23 medium second 2  3
24 high   second 3  3
25 medium first  4  3

Find the sin of each value in columns if some columns are categorical

Using numcolwise function from plyr package to find the sin of each value in columns if some columns are categorical in the data frame df −

Level<-sample(c("low","medium","high"),25,replace=TRUE)
Group<-sample(c("first","second"),25,replace=TRUE)
DV1<-sample(1:5,25,replace=TRUE)
DV2<-sample(1:5,25,replace=TRUE)
df<-data.frame(Level,Group,DV1,DV2)
library(plyr)
numcolwise(sin)(df)

Output

        DV1    DV2
1   0.9092974  0.1411200
2  -0.9589243 -0.7568025
3   0.9092974  0.1411200
4   0.1411200 -0.7568025
5  -0.7568025 -0.7568025
6  -0.7568025  0.8414710
7   0.8414710  0.1411200
8   0.8414710  0.8414710
9   0.1411200 -0.9589243
10 -0.7568025 -0.9589243
11 -0.9589243  0.1411200
12 -0.9589243  0.8414710
13 -0.7568025  0.9092974
14  0.8414710  0.1411200
15  0.8414710 -0.7568025
16  0.1411200  0.8414710
17  0.1411200  0.1411200
18  0.9092974  0.9092974
19  0.9092974  0.1411200
20 -0.9589243  0.8414710
21  0.9092974  0.1411200
22 -0.7568025  0.9092974
23 -0.7568025 -0.9589243
24 -0.7568025  0.9092974
25 -0.7568025  0.1411200

Updated on: 15-Nov-2021

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements