简体   繁体   中英

Convert Column Data to Rows in R

Input :

    Col_A                Col_B
1   Samsung_Note           10
2   Samsung_Notebook       20
3   Samsung_Tablet_Device  30
4   Note                   40

Expected Output:

Col_A           Col_B
Samsung          10
Note             10
Samsung_Note     10
Samsung          20
Notebook         20
Samsung_Notebook 20
Samsung          30
Tablet           30
Device           30
Samsung_Tablet   30
Tablet_Device    30
Samsung_Device   30
Note             40

I would like to change my data as per provided expectations. Please suggest an optimized way to perform this operation.

For this particular purpose please assume x_z = z_x

maybe there's an easier way, but this should work:

elements <- strsplit(df$COL_A, "_")
elementsAll <- lapply(seq_along(elements), function(i) append(elements[[i]], df$COL_A[i]))
dfTemp <- data.frame(
    V1 = unlist(elementsAll), 
    V2 = rep(unlist(lapply(elementsAll, function(x) x[length(x)])), 
             unlist(lapply(elementsAll, length)))
)
dfTemp <- dfTemp[!duplicated(dfTemp),]
desiredDF <- merge(df, dfTemp, by.x = "COL_A", by.y = V2)

Where df denotes the input data frame. Make sure that COL_A is not a factor but a character!

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