简体   繁体   中英

Read and input data directly from csv

I have the data available in the below format & instead of adding the nos. manually, I want to read them directly from the csv file. Please check the **"Total Clicks" ** and **"Version # Opens" ** section. Thanks in advance

Data I have in .csv format:

Total Clicks                
Section   Version A  Version B  Version C   Version D  
Section1    1,999   2,116              2,307    2,568
Section2    3,450   1,781              3,416    1,399
Section3    1,773     915              1,744      644
Section4        0   2,255                  0    1,432
Section5      588     573                721      235
Main email  7,222   7,067              7,467    6,043
Total email 7,810   7,640              8,188    6,278

Version # Opens
A    9,073
B    9,150
C    9,215
D    9,153

Currently I am assigning the data manually in the below format:

S1_Click_A=1,999 ####(section 1, email A)
S1_Click_B=2,116 ## (section 1, email B)
S1_Click_C=2,307
S1_Click_D=2,568
S2_Click_A=3,450 
S2_Click_B=1,781 
.
.
.
S5_Click_C=721 
S5_Click_D=235
MainBody_Click_A=7,222
MainBody_Click_B=7,067
.
.
TotalEmail_Click_C=8,188
TotalEmail_Click_D=6,278

I expect a code which helps to pick the data directly from the file instead of doing this manually.

I would say that you could do something like this:

library(tidyverse)
wide_file <- read_csv("you_file.csv")

# Gather to make it long

long_file <- wide_file %>%
    gather(version, value, -Section)

# Now make your new column using unite (basically pastes columns together)

long_file_2 <- long_file %>%
    unite("new_col", id:version, sep = "_)

That should give you the data format that you have specified

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM